$ grep pattyo /etc/passwd
pattyo:x:500:500:PattyO:/home/pattyo:/bin/tcsh
1. checks to see if it matches an internal shell commandEvery shell has a few built in commands.2. checks to see if the command is an alias for another commandThe alias command will list all the aliases defined in your environment.3. searches the hard disk for the program based on your PATH$ alias
alias mv='mv -i'
alias new='ls -lt|head'What is your path?
$ envWhat happens when a command is not in your path?
...
PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:\
/home/pattyo/bin:/sbin:/usr/sbin
...The env command displays your environment.
This is also the environment you get when you su - to some other user on the system.$ fortuneUse the whereis command to locate the command.
bash: fortune: command not found$ whereis fortuneNow use the absolute (full) path name to execute the program.
fortune: /usr/games/fortune /usr/share/man/man6/fortune.6.gz$ /usr/games/fortune
Art is a lie which makes us realize the truth.
-- Picasso
Major UNIX shells:
$ file /etc/init.d/*
...
/etc/init.d/sshd: Bourne-Again shell script
text executable
...
$ head -1 /etc/init.d/sshd
#!/bin/bash
! # $ % ^ & * } ( ) [ ] | \ ; < > ? /
$ touch news new nest net ne nac pack n $ cp n* doc $ ls doc n nac ne nest new news
Echo all files whose file names begin wiht test1 to the screen:
$ touch test1 test2 test1.1 test1.2 test1.3 $ echo test1* test1 test1.2 test1.3How do you match characters in the middle and beginning of files?
$ touch baba aba $ ls *a* aba baba nac pack $ ls a* aba $ ls *a aba baba $ ls a*a aba
The exception is that you won't
get a match with files that begin with a dot (.)
If you want to match hidden files
(beginning with '.') you need to put '.' in the search string:
$ cd $ echo .bash* .bash_history .bash_logout .bash_profile .bashrc
Note: You can use the echo command to list the files in a directory. The echo command is built into the shell. Question Mark ?
$ mkdir test $ cd test $ touch net new few news $ ls ne? net new $ rm -i ?e? rm: remove `few'? y rm: remove `net'? y rm: remove `new'? y
$ ls /etc/????wd /etc/passwd
$ echo ?bash* ?bash*Lists [ ]
Note: Only one of s, t, or w is mached
$ echo [!tn]*
myfile few
The position defined by the location of the square brackets is expanded by the shell but to only one character.
$ ls /etc/rc.d/rc3.d/K[1-5]*Remember, the symbolic links found in this directory are executed based on the run-level your system is moving to (S for start, K for kill).
K20bootparamd K20rstatd K20rusersd K20rwhod K28amd K35dhcpd K55routed
$ echo "*.*" *.* $ echo *.* prints a listing of the files in your current directory
| Single quotes (' ') | ignore all metacharacters between quotes | echo '$HOME'
$HOME |
| Double quotes (" ") | ignore all metacharacters except $, `, and \ | echo "$HOME"
/home/pattyo |
| Backslash (\) | ignore the special meaning of the next metacharacter | echo \$HOME
$HOME |
The mail program is instructed to take someletter as the standard input:
$ mail someuser < somefileOutput Redirection0 stdin: somefile (changed)
1 stdout (unchanged)-- the screen
2 stderr (unchanged)-- the screen$ mail someuser -s "my subject" < somefile
$ who > filenameThe shell creates the new file, filename, and connects the output of the who utility to the new file.
NOTE: If filename already existed, it will be deleted as soon as the who command executes!
$ cal 10 2001 > thismonthThe cal command's descriptor table follows:
$ cat thismonth
October 2001
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
$ who > whoisthere
$ date >> whoisthere
$ who >> whoisthere
$ cat whoisthere
$ ls -l > ls.outThe sort command is followed by the -n option for numerical data and the "+4" option which tells sort to sort the file based on the 4th column (*starting from 0).
$ sort -n +4 < ls.out
$ cd /tmpNow examine the contents of the file, 6, you just created:
$ echo 2 * 3 > 6 more stuff here
$ cat 6You instructed the shell to echo the number 2, list all the files in the current directory, echo the number 3, then redirect everything to the file, 6.
What would be the effect of quoting this command the way you see below?
$ echo '2 * 3 > 6 more stuff here'
Look at the following three commands. What do they have in common?
$ cat < file1 > file2Using cat and Redirecting Output
$ cp file1 file2
$ cat file1 > file2
The cat command is normally used to list the contents of a file
$ cat /etc/passwdYou can use cat with redirection to create a new file:
$ cat > newfileHere are the descriptores for the cat > newfile example:
some text here
some more text here
<Ctrl>d
0 stdin (unchanged)-- keyboardThis example creates a file called file3 which contains the combined contents of file1 and file2.
1 stdout: newfile (changed)
2 stderr (unchanged)-- screen
$ cat file1 file2 > file3<Is it possible to concatinate two files into one file and retain the same name as one of the original files?
example 1:
By default, the standard error ouput of a command goes to the screen or the system console.
If you want to save the error messages you can append the redirector to standard output of your command.
$ command 2> file1Lets try catting a file that doesn't exist:
$ cat dogNow save the error output to a file:
cat: dog: No such file or directory
Sending standard error to the bit bucket. /dev/null is the bit bucket. What goes in, never comes out. You won't see the error message on the screen.
$ cat dog 2> /dev/nullRedirecting and appending error output
Simply add the '2' file descriptor.
command 2>> filenameCombined Redirection
command < infile > outfile 2> errfile
command >> apendfile 2>> errfile < infile
$ sort > /tmp/myoutput < /etc/passwd 2> /tmp/myerrs $ sort >> /tmp/myoutput < /etc/passwd 2>> /tmp/myerrs
$ command > logfile 2>&1
$ command 2>&1 > outfile
$ cat dog junk 2>&1 > outfile
cat: dog: No such file or directory
$ cat dog junk > logfile 2>&1
$ cat logfile
cat: dog: No such file or directory
File descriptor defaults: 1> is equivalent to >, and 0< is the same as <.
These commands are essentially the same:
$ date 1> now
$ date > now
cmd1 | cmd2
$ ls -lt | head
$ ls | wc -w
$ ls -l > ls.outHere is a way to put those two commands together using a pipe:
$ sort +4 < ls.out
$ ls -l | sort +4Using Filters
$ ls | grep
^n | wc -l
4
Warning
To see the output on the screen and also save it to a file:
$ date | tee mynow_fileThe tee command can be used for capturing a snapshot of information at a specific point in a pipe.
You can send both stderr and stdout through a pipe.
$ command 2>&1 | tee logfile
The Backslash for Escaping Special Characters
Since the single quote is a special character, how do you echo a string that contains a quote such as this:
$ echo my name's pattyYou would have to escape the quote.
$ echo my name\'s pattyLine Continuations
my name's patty
This shell feature is used when the options and arguments appended to a single command cause you to type past the lenght of the line.
It is typically used in shell scripting to make the script more readable.
$ cmd continued_cmd \Have you ever wondered why your prompt looked like this after typing a command?
> continued_cmd
>The return key is just another character that can be escaped. You might have hit the backslash by accident. In this case, the shell is waiting for more input.
What if you really want the backslash in your output.
$ echo i like the \ character
$ echo i like the \\ character