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:

ShellScript
Ctrl +n:sameasDownarrow.Ctrl +p:sameasUparrow.Ctrl +r:beginsabackwardsearchthroughcommandhistory.(keep pressingCtrl+rtomovebackward)Ctrl +s:tostopoutputtoterminal.Ctrl +q:toresumeoutputtoterminalafterCtrl+s.Ctrl +a:movetothebeginningofline.Ctrl +e:movetotheendofline.Ctrl +d:ifyou'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 itCtrl + u : cut the line before the cursor; then Ctrl + y paste itCtrl + _ : 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:

ShellScript
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.

ShellScript
# '*' 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 patternsls *.{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:

  1. source .env – This command loads the variables from the .env file into the current shell session.
  2. export VAR_NAME=VALUE – This command creates a new environment variable and sets its value.
  3. echo $VAR_NAME – This command prints the value of the specified environment variable.
  4. 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.

ShellScript
$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 forthe shell.$$:pid of the current shell (not subshell).$!:is thePIDofthemostrecentbackgroundcommand.$DESKTOP_SESSIONcurrent display manager$EDITORpreferred text editor.$LANGcurrent language.$PATHlist of directories to search forexecutable files (i.e. ready-to-runprograms)$PWDcurrent directory$SHELLcurrent shell$USERcurrent username$HOSTNAMEcurrent hostname
Elsewhere On TurboGeek:  Top Terraform Q&A: Questions and Answers

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

Leave a Reply

Your email address will not be published.Required fields are marked *

Translate »