Linux: Moving ahead
Shell Configuration Files
Calling order of Bash Configuration Files – Login Shell
During a login shell, these configuration script files are called in this order:
First: /etc/profile
Then the order branches out, the first file that is found is used, all the others are ignored even if they exist:
- ~/.bash_profile
- ~/.bash_login
- ~/.profile
Next, .bashrc, followed by /etc/bashrc
/etc/bashrc takes care of the system-wide functions and aliases.
/etc/profile: System-wide environment and startup programs, used during a login shell.
/etc/profile.dl: location of extra environment setup scripts.
The following files are in the home directory of the user (Note that not all distributions will use all these files)
- Bash_profile: use to set user-specific shell environment preferences.
- Bash_logout: Runs when the user logs out of a login shell and not from the terminal, it does not exist on every system.
- Bashrc: It’s a non-login file that stores user-specific functions and aliases.
ORDER:
/etc/profile > ~/.bash_profile > ~/.bashrc > /etc/bashrc
SHELL VARIABLES:
Command: $ env: Lists out environment variables of the currently logged in shell.
Command: $ echo $VARIABLE: Prints the value of variables to the screen.
The format for declaring a new variable in Bash:
$ VARIABLENAME=value
How to export variable and its value to other Shells:
$ exportVARIABLE
How to print previous WORKING DIRECTORY to the terminal:
$ echo $OLDPWD
Change to previous working directory:
$ cd –
Many times, we store applications to be executed in /opt folder, but that is not listed if we run command:
$ echo $PATH
Output: /usr/local/sbin: /usr/local/bin: /usr/sbin: /usr/bin: /sbin: /bin: /usr/local/game: /snap/bin
If we want to add a new directory into this path the following command, we need to execute:
VARIABLENAME=value
PATH=$PATH:/opt (: Directory separator in path variable values, /opt the directory we want to add into path.
Now, if run the same command:
$ echo $PATH
Output: /usr/local/sbin: /usr/local/bin: /usr/sbin: /usr/bin: /sbin: /bin: /usr/local/games: /snap/bin: /opt
GLOBBING:
- * – Matches zero or more characters.
- ? – Matches any single character.
- [abc] – Matches any one of the characters in the list, case sensitive.
- [^abc] – Matches any one of the characters in the list except those mentioned in the pattern
- [0-9] – Matches a range of numbers.
If you looking to search for some documents inside many directories, you know the file name but don’t remember the directory in which you have placed it, you can utilize GLOBBING there:
Command: $ ls directory_name/*/file_name (* for the directories inside directories whose name we are not sure about.)
QUOTING:
- ““: Double quotes, contains strings and any variables or commands within them get
evaluated or acted on.
- ‘’: Single quotes, anything within these gets treated literally, disable any special character
functionality.
- \: Backslash, escape character, disables any special character functionality that immediately
follows it.
- Quotes around the spaces or an escape character preceding a space will be treated literally.
Command Formatting:
Three parts
1 2 3
ls -l Documents/
(command) (options) (arguments)
- Command: What to do?
- Options: How to do it?
- Arguments: What to do it on?
Locate, find and whreis Commands:
- $ locate: Searches a local database of files and folders looking for items that match the search criteria. E.g.: locate cd
- $ find: Searches the file system for files that match the search criteria find /path/to/folder
-name file
When using the find command to search for part of a file name, use globbing within single quotes:
$ find /path/to/folder -name ‘*file’ (-name: switch)
- $ whereis: Locates binary, source and/or manual pages for a command.