Executing commands is an integral part of using any Linux system. To follow up on my post about navigating file systems from the command line ( Linux Tips: File & Directory Handling ) more effectively, I am I have picked up over the years.
Discovering What Shell You Are In
Finding exactly out what shell you are currently using in a terminal can be determined using a variety of different techniques. Some of which are:
Using the ps command to display the name of the shell process ( $ is a shell variable that reflects the pid of the current shell ).
ps -o command $$
You can echo the $0 variable which will display the name of the shell script.
echo $0
Or you can also echo the $SHELL environment variable which will display your shell script
echo $SHELL
Process Handling
Sometimes for various reasons you want like to exit a program before it has a chance to run to completion. This can be achieved by pressing the “CTRL + C” keys on the keyboard to send a TERM signal to the running process. Since the TERM signal can be blocked or ignored by a process you may want to terminate a process by pressing the “CTRL + ” keys which will send a QUIT signal to the running process instead.
If you don’t want to end the process completely you can press the “CTRL + Z” keys this will simply suspend the programs execution. You can then resume execution of the program again but in the background by typing bg. Or resume running the last suspended process in the foreground with fg. To see any suspended processes you can use the jobs command which will display something similar to:
[1]- Stopped tar cjvf file.tar.bz2 video/
[2]+ Stopped tar cjvf file2.tar.bz2 music/
We can see from the output from the jobs command above that I currently have two jobs which I have suspended. The number at the start of each line is the job number for the suspended proces. This job number can then be used in conjunction with the bg or fg commands to control a particular job, rather than just the last suspended job. For example to resume my job compressing the video/ folder in the foreground I could execute:
fg %1
or simply
%1
Command History
Learning how to effectively navigate commands you have executed in the past saves alot of typing. The BASH shell helps by maintaining a history file that contains a list of commands you have run in the past. The terminal history is stored in RAM during a session and written to the history file after a user logs out of their session. You can find the location of the history file by looking at the shell variable HISTFILE i.e
echo $HISTFILE
In this case my history file is shown as being located at /home/anthony/.bash_history.
If you would like to see your command history, simply use the history command without any arguments to output the contents of your history file with your commands ordered from oldest to newest. If for some reason you would like to clear your command history you simply add the -c flag to the history command i.e
history -c
At the command prompt one effective way of saving yourself some typing is pressing the “CTRL + R” keys. This will trigger a reverse command search of your history simply start typing the command you want to narrow your search down to the command you want and then simply press enter to execute.
Which brings us to the BASH bang operator (!), this allows you to run previously executed commands. Its very flexible and allows you to specify a command that was previously run within the terminal in a variety of ways for example:
Following the bang with a dash and a number will execute a command, X number of commands back in the terminal history. To execute the second last command you would run:
!-2
The bang operator will also let you to specify a particular line number in your history file to run. Tou run the command on line 478 of your terminal history enter:
!487
Run the most recent command in history that starts with a command. For example to reconnect to a server you were previously logged into:
!slogin
If you are using the above format to rerun a the slogin by name again but are not sure what arguments you used with the command. You can use the “:p” extension first this will print the complete command and arguments that would be executed:
!slogin:p
To simply repeat the last command executed you can press the up arrow key on the keyboard or use the “!!” (otherwise known as bang bang).
TIP:ย The double bang ( !! ) is extremely handy when you are logged in as a normal user, you have typed a command that requires root privileges, but forgotten to use sudo. To simply run the command again using sudo simply enter “sudo !!” at the command prompt.
Argument Reuse
The bang operator can also be used in conjunction with a dollar sign ($) as a handy way to reuse the arguments provided to the previously executed command. For example if you executed:
ls /var/log/
You could then change into the /var/log directory with minimal typing by executing:
cd !$
If you like you can also check the arguments before using them with a command you can use the “:p” again with the bang operator to simply print the value i.e
Executing !$ after the previous example will output to the terminal:
/var/log
Your Top 100
Not really that useful, but interesting regardless. Running the snippet below will give you a list of your 100 most used commands from the terminal history:
history | sed "s/^[0-9 ]*//" | sed "s/ *| */n/g" | awk '{print $1}' | sort | uniq -c | sort -rn | head -n 100
With the number in front of each command being a count of how many times the command appears in the terminal history.
Further Reading