Bash CLI Terminal Tips and Tricks
Using CTRL Commands
In the Bash shell, there are several useful CTRL commands that can help you navigate and manage your command line environment. Here are some of the most common Bash CTRL commands:
Ctrl + n : same as Down arrow.
Ctrl + p : same as Up arrow.
Ctrl + r : begins a backward search through command history.(keep pressing Ctrl + r to move backward)
Ctrl + s : to stop output to terminal.
Ctrl + q : to resume output to terminal after Ctrl + s.
Ctrl + a : move to the beginning of line.
Ctrl + e : move to the end of line.
Ctrl + d : if you've type something, Ctrl + d deletes the character under the cursor, else, it escapes the current shell.
Ctrl + k : delete all text from the cursor to the end of line.
Ctrl + x + backspace : delete all text from the beginning of line to the cursor.
Ctrl + t : transpose the character before the cursor with the one under the cursor, press Esc + t to transposes the two words before the cursor.
Ctrl + w : cut the word before the cursor; then Ctrl + y paste it
Ctrl + u : cut the line before the cursor; then Ctrl + y paste it
Ctrl + _ : undo typing.
Ctrl + l : equivalent to clear.
Ctrl + x + Ctrl + e : launch editor defined by $EDITOR to input your command. Useful for multi-line commands.
AlphaNumeric Edits
AlphaNumeric Edits are a set of commands that allow you to quickly edit or manipulate text on the command line. These commands use alphanumeric characters as shortcuts to perform specific editing functions. Here are some of the most common AlphaNumeric Edits in BASH:
Esc + u
# converts text from cursor to the end of the word to uppercase.
Esc + l
# converts text from cursor to the end of the word to lowercase.
Esc + c
# converts letter under the cursor to uppercase, rest of the word to lowercase.
Bash Globbing
In Bash, globbing is the process of expanding wildcard characters (also known as metacharacters) in file and directory names to match existing files and directories. The most commonly used wildcard characters are the asterisk (*) and the question mark (?).
For example, suppose you have a directory that contains three files: file1.txt, file2.txt, and file3.txt. If you use the command “ls file*.txt”, the shell will expand the “*” character to match any file names that start with “file” and end with “.txt”. Therefore, the command will be expanded to “ls file1.txt file2.txt file3.txt” and will list all three files.
# '*' serves as a "wild card" for filename expansion.
/etc/pa*wd #/etc/passwd
# '?' serves as a single-character "wild card" for filename expansion.
/b?n/?at #/bin/cat
# '[]' serves to match the character from a range.
ls -l [a-z]* #list all files with alphabet in its filename.
# '{}' can be used to match filenames with more than one patterns
ls *.{sh,py} #list all .sh and .py files
Environmental (.env)
In Bash, an environment file (often named “.env”) is a file that contains environment variables that can be loaded into the shell’s environment. Environment variables are variables that are used to store information about the current shell session, such as system settings or user preferences.
The .env file can contain variables in the format of “VAR_NAME=VALUE”. Here are some common commands used with environment files in Bash:
source .env
– This command loads the variables from the .env file into the current shell session.export VAR_NAME=VALUE
– This command creates a new environment variable and sets its value.echo $VAR_NAME
– This command prints the value of the specified environment variable.unset VAR_NAME
– This command removes the specified environment variable.
Using an environment file can be useful for storing and managing environment variables that are used across multiple shell sessions or scripts. It also allows you to keep sensitive information (such as API keys or passwords) separate from your code and easily change them when needed without modifying your scripts.
$0 :name of shell or shell script.
$1, $2, $3, ... :positional parameters.
$# :number of positional parameters.
$? :most recent foreground pipeline exit status.
$- :current options set for the shell.
$$ :pid of the current shell (not subshell).
$! :is the PID of the most recent background command.
$DESKTOP_SESSION current display manager
$EDITOR preferred text editor.
$LANG current language.
$PATH list of directories to search for executable files (i.e. ready-to-run programs)
$PWD current directory
$SHELL current shell
$USER current username
$HOSTNAME current hostname
Recent Comments