Thursday, November 17, 2022

Some bash keyboard shortcuts and special characters

Some bash keyboard shortcuts


  • Esc + T: Swap the last two words before the cursor
  • Ctrl + H: Delete the letter starting at the cursor
  • Ctrl + W: Delete the word starting at the cursor
  • TAB: Auto-complete files, directory, command names and much more
  • Ctrl + R: To see the command history.
  • Ctrl + U: Clear the line
  • Ctrl + C: Cancel currently running commands.
  • Ctrl + L: Clear the screen
  • Ctrl + T: Swap the last two characters before the cursor

Some bash special characters

Characters Description
/ Directory separator, used to separate a string of directory names. Example: /home/projects/file
\ Escape character. If you want to reference a special character, you must “escape” it with a backslash first. Example: \n means newline; \v means vertical tab; \r means return
# Lines starting with # will not be executed. These lines are comments
. Current directory. When its the first character in a filename, it can also “hide” files
.. Returns the parent directory
~ Returns user’s home directory
~+ Returns the current working directory. It corresponds to the $PWD internal variable
~- Returns the previous working directory. It corresponds to the $OLDPWD internal variable
* Represents 0 or more characters in a filename, or by itself, it matches all files in a directory. Example: file*2019 can return: file2019, file_comp2019, fileMay2019
[] Can be used to represent a range of values, e.g. [0-9], [A-Z], etc. Example: file[3-5].txt represents file3.txt, file4.txt, file5.txt
| Known as “pipe". It redirects the output of the previous command into the input of the next command. Example: ls | less
< It redirects a file as an input to a program. Example: more < file.txt
> In script name >filename it will redirect the output of “script name” to “file filename”. Overwrite filename if it already exists. Example: ls > file.txt
>> Redirect and append the output of the command to the end of the file. Example: echo "To the end of file" >> file.txt
& Execute a job in the background and immediately get your shell back. Example: sleep 10 &
&& “AND logical operator”. It returns (success) only if both the linked test conditions are true. It would run the second command only if the first one ran without errors. Example: let "num = (( 0 && 1 ))"; cd/comp/projs && less messages
; “Command separator”. Allows you to execute multiple commands in a single line. Example: cd/comp/projs ; less messages
? This character serves as a single character in a filename. Example: file?.txt can represent file1.txt, file2.txt, file3.txt

 

No comments:

Post a Comment