Basic/Common Special symbols/Special characters in Linux

Last Updated on

Home Directory ~

# Back to home directory
cd ~
 
# Change to sub directory within home directory
cd ~/Desktop

Current Directory .

ls -al
ls -al

. in above image means current directory

# Run script.sh in current directory
./script.sh

Parent Directory ..

cd ..
cd ..
cd ..
# Change to /test/ folder in parent folder directory
cd ../test/

Path Separator, System root folder /

ls /etc
# Change to system root folder
cd /

Comment #

#Comments are ignored by Bash shell
Comment
Comment

We can also comment part of the string from variable

# Define variable
test_string="abc def"
 
# echo the variable with abc being commented out
echo ghi ${test_string#abc}
 
# echo full variable
echo $test_string
Comment part of the string/variable
Comment part of the string/variable

Single Character Wildcard ?

? Can be used for this purpose

ls file?.txt
ls file?.txt
ls file?.txt

Since file.txt doesn’t have any characters right after file, it is not in the result.

To match exactly how many characters with ?, we use corresponding number of ?

ls ????.txt
ls ????.txt
ls ????.txt

Wildcard *

Match any character including space

ls file*
ls file*
ls file*

Match any type of files

ls file.*

Character Set Wildcard []

Match at least one of the characters in the []

ls file0[123].txt
ls file0[123].txt
ls file0[123].txt
ls file[012][012].txt
ls file[012][012].txt

Shell Command Separator (Run till the last one anyway) ;

command 1; command 2; command 3
command 1; command 2; command 3
command 1; command 2; command 3

When using Shell Command Separator “;” no matter the previous command succeeded or not, it will run till the last command

Shell Command Separator (Stop if there is error/failed) &&

command 1 && command 2 && command 3
command 1 && command 2 && command 3
command 1 && command 2 && command 3

Background Process &

Add & behind the command to run the command in the background

command &
Run the command in the background
Run the command in the background

1709 is the process ID of this background task

Input Redirection <

command < file
sort test file
sort test file
wc test
 
wx < test

(< does not display source filename)

Output Redirection >

ls > ListOfFiles.txt
 
# To read the file again
cat ListOfFiles.txt

It can be used with stderr, stdin, stdout (See bottom of this page)

cat test.txt 2> err.txt
Use with stderr
Use with stderr

Pipe |

command 1 | command 2 | command 3
cat test | grep [Aa] | sort -r
Demo or pipe
Demo or pipe

1 Use cat to read test, pip to grep command

2 Use grep to filter the test, only left with text including A and a, pipe to sort command

3 Use sort to reverse sort the text

Pipeline logical NOT and History Operator !

[ ! -d ./testfolder ] && echo "Folder testfolder does not exist"

[ ! -d ./testfolder ]: Check if the folder named testfolder exists

If does not exist, echo the text Folder testfolder does not exist

Run history command

!number
 
# e.g.
!210
Run history command
Run history command
# Run last command
!!
Run last command
Run last command

Variable Expressions $

In Bash shell, $ usually means variables

We can use echo to view the values of the variable

echo $PATH
 
echo $USER
 
echo $HOME
Show values in variables
Show values in variables

We can define variables in following ways

year=2020
MyName=Fred
Define variable

We can use {} to perform other advanced manipulation of text

# Define variable
string=12345qwert
 
# Output the string
echo ${string}
 
# Output text begin from index of 3 (Begin from 0)
echo ${string:3}
 
# Output 3 characters begin from index 0
echo ${string:0:3}
 
# Output 2 characters begin from index 2
echo ${string:2:2}

Quoting Special Characters “” ”

Use “” to stop special character functions (But not $)

echo "$string"
Double quotes
Double quotes

Use single quotes ” to disable all special character’s function

echo '$string'
Single quotes
Single quotes

We can also use backslash \ to prevent the following character to be functioning as a special character

echo "\$string"
Backslash
Backslash

Extend Reading

More on redirection, pipeline,stdin,stdout,stderr | Linux – Basics, Useful Terminal commands and Basic File manipulation, (QuickStart)

More on flow control, if else, loop, variable Linux Bash/Shell simple, basic flow control


Leave a Reply

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