Introduction to BASH in Mac OS X
You can check what shell you are running by using below command.
If you see the output as /bin/bash, it means that you are using the BASH Shell.
To find out all commands starting with specific letter or letters, you can type the letter and press the TAB key twice.
Also you do not need to enter the full directory or file name. You can type first few characters of the file and then press tab key. BASH will auto complete the file name automatically provided it is able to identify the file uniquely.
For example – let us say we have 2 directories with name bigcities and bigcountries. Then to change the directory to bigcities, you can use below command.
and then press TAB key. Directory name will be auto-completed. But if type only “big” and then press TAB key, name will not be auto-completed because BASH does not find the unique directory name.
Getting out of shell
To exit the shell you can use below command
You must know below BASH shortcuts. Note that if you are using windows keyboard, you will have to press “CTRL” key.
- command-key+c – This hotkey sends SIGINT singal (Interrupt signal) to the application. It is up to the application if it has to terminate itself.
- command-key+z – This hotkey sends SIGTSTP singal (Stop signal) to the foreground application. Application is sent to the background in suspended mode.
You can view all application that are in background using jobs command.
- command-key+a – sends the cursor to the start of the command line.
- command-key+e – sends the cursor to the end of the command line.
- command-key+u – Cut the left portion of command from the cursor position.
- command-key+k – Cut the right portion of command from the cursor position.
- command-key+r – Search for the commands from command history and execute it.
- ALT+. – Access the last argument of previous command on the command line.
- command-key+w – Delete one word from the command line
BASH tips and tricks
We can run 2 commands one after another using below syntax.
cmd1 ; cmd2
If you want to run second command only if first one is successful, you can use below syntax.
cmd1 && cmd2
If you want to run second command only if first one fails, you can use below syntax.
cmd1 || cmd2
You can run the command in background using below syntax.