A Study in Bash Shell: Executing Commands

A Study in Bash Shell: Executing Commands

Running Commands

ubuntu@ip-172-31-24-188:~$ date
Tue Jan 19 02:45:27 UTC 2016
ubuntu@ip-172-31-24-188:~$ pwd
/home/ubuntu
ubuntu@ip-172-31-24-188:~$ hostname
ip-172-31-24-188
ubuntu@ip-172-31-24-188:~$ ls
git  setup.sh  There
ubuntu@ip-172-31-24-188:~$ ls -t
There  setup.sh  git
ubuntu@ip-172-31-24-188:~$ ls -lt
total 16
-rw-rw-r-- 1 ubuntu ubuntu     0 Jan 19 02:33 There
-rw-rw-r-- 1 ubuntu ubuntu 10338 Jan 17 07:00 setup.sh
drwxrwxr-x 3 ubuntu ubuntu  4096 Jan 16 17:03 git

History Commands

ubuntu@ip-172-31-24-188:~$ history 8
   77  pwd
   78  hostname
   79  ls
   80  ls -t
   81  ls -lt
   82  ls -alt
   83  history
   84  history 8

Connecting and Expanding Commands

A truly powerful feature of the shell is the capability to redirect the input and output of commands to and from other commands and fi les.

Piping between commands

Here is an example of a command line that includes pipes:
$ cat /etc/passwd | sort | less
This command lists the contents of the /etc/passwd fi le and pipes the output to the
sort command. The sort command takes the usernames that begin each line of the
/etc/passwd fi le, sorts them alphabetically, and pipes the output to the less command (to page through the output).

Sequential commands

yang@ubuntu:~$ date ; who am i; date
Tue Jan 19 11:09:26 CST 2016
yang     pts/1        2016-01-19 10:24 (u6036458-tpl-a.ten.thomsonreuters.com)
Tue Jan 19 11:09:26 CST 2016

Background commands

yang@ubuntu:~$ date &
[1] 20243
yang@ubuntu:~$ Tue Jan 19 11:11:50 CST 2016

[1]+  Done                    date

Expanding commands

vi $(find /home | grep xyzzy)