Skip to content

Linux Commands: Shell and Text Processing

Shell built-ins and scripting, keyboard shortcuts, redirection, environment variables and text tools.

Part of the Linux commands cheat sheet.

Shell Built-in and Scripting Commands

alias — Define or display aliases:

alias ll='ls -l'

bind — Set Readline key bindings and variables:

bind -P

break — Exit from a for, while, or until loop:

break

builtin — Run a shell builtin, passing it args:

builtin cd /tmp

case — Conditional construct in shell scripts:

case $VAR in ... esac

continue — Resume the next iteration of a loop:

continue

declare — Set variables and attributes:

declare -i NUM=5

enable — Enable and disable shell builtins:

enable -n cd

env — Set environment and execute command, or print environment:

env | grep USER

eval — Construct command by concatenating arguments:

eval "ls -l"

exec — Replace the shell with the given command:

exec bash

exit — Exit the shell:

exit 0

expect — Programmed dialogue with interactive programs:

expect script.exp

export — Set export attribute for shell variables:

export PATH=$PATH:/new/dir

expr — Evaluate expressions:

expr 5 + 3

factor — Print the prime factors:

factor 100

fc — Process command history list:

fc -l

function — Define shell functions:

function myfunc() { echo "hi"; }

for — Loop construct:

for i in {1..5}; do echo $i; done

if — Conditional construct:

if [ -f file ]; then echo "Exists"; fi

let — Evaluate arithmetic expressions:

let "a = 5 + 3"

printf — Format and print data:

`printf "Result: %04d

read — Read a line from standard input:

read -p "Enter name: " name

return — Return from a shell function:

return 1

select — Generate menus from list of words:

select opt in "A" "B"; do echo $opt; break; done

seq — Print a sequence of numbers:

seq 1 5

setsid — Run a program in a new session:

setsid my_script.sh

shift — Shift positional parameters:

shift 2

source — Execute commands from a file in the current shell:

source ~/.bashrc

type — Display information about command type:

type ls

until — Loop construct:

until [ $i -gt 5 ]; do ... done

while — Loop construct:

while true; do ... done

yes — Output a string repeatedly until killed:

yes "y" | rm -r dir/

sudo — Execute a command as another user:

sudo dnf update

sleep — Delay for a specified amount of time:

sleep 5

Bash Shortcuts Commands

Shortcut Description
Ctrl + A Move to the beginning of the line
Ctrl + E Move to the end of the line
Ctrl + B Move back one character
Ctrl + F Move forward one character
Alt + B Move back one word
Alt + F Move forward one word

Editing Shortcuts

Shortcut Description
Ctrl + U Cut/delete text from the cursor to the beginning of the line
Ctrl + K Cut/delete text from the cursor to the end of the line
Ctrl + W Cut/delete the word before the cursor
Ctrl + Y Paste the last cut text
Ctrl + L Clear the terminal screen
Ctrl + C Terminate the currently running command

History Shortcuts

Shortcut Description
Ctrl + R Search command history (reverse search)
Ctrl + G Exit history search mode
Ctrl + P Go to the previous command in history
Ctrl + N Go to the next command in history

IO Redirection Commands

cmd < file — Redirect input:

sort < names.txt

cmd > file — Redirect output (overwrite):

echo "Hello" > out.txt

cmd >> file — Redirect output (append):

echo "World" >> out.txt

cmd 2> file — Redirect error output:

ls no_exist_dir 2> errors.txt

cmd 2>&1 — Redirect stderr to stdout:

make 2>&1 | tee build.log

cmd &> file — Redirect stdout and stderr:

make &> build.log

cmd 1>&2 — Redirect stdout to stderr:

echo "Error!" 1>&2

cmd > /dev/null — Discard standard output:

ping 8.8.8.8 > /dev/null

cmd1 <(cmd2) — Process substitution:

diff <(ls dir1) <(ls dir2)

Environment Variable Commands

export VARIABLE_NAME=value — Set and export an environment variable:

export PATH=$PATH:/opt/bin

echo $VARIABLE_NAME — Display value of a variable:

echo $USER

env — List all environment variables:

env

unset VARIABLE_NAME — Remove an environment variable:

unset JAVA_HOME

export -p — List all exported variables:

export -p

env VAR1=value COMMAND — Set variable temporarily for a command:

env LC_ALL=C sort file.txt

printenv — Display environment variables:

printenv PATH

Text Processing and Formatting Commands

awk — Pattern scanning and processing language:

awk '{print $1}' file.txt

aspell — Interactive spell checker:

aspell check file.txt

banner — Print large banner on printer/stdout:

banner Hello

bc — An arbitrary precision calculator language:

echo "10 + 5" | bc

col — Filter reverse line feeds from input:

man ls | col -b > ls.txt

colcrt — Filter nroff output for CRT previewing:

colcrt file.txt

colrm — Remove columns from a file:

colrm 1 5 < file.txt

column — Columnate lists:

mount | column -t

dc — An arbitrary precision calculator:

dc -e '10 5 + p'

egrep — Print lines matching a pattern (grep -E):

egrep "pattern1|pattern2" file.txt

fgrep — Print lines matching a pattern (grep -F):

fgrep "exact string" file.txt

fmt — Simple optimal text formatter:

fmt -w 60 file.txt

grep — Print lines matching a pattern:

grep "error" /var/log/syslog

sdiff — Side-by-side merge of file differences:

sdiff file1.txt file2.txt

sed — Stream editor for filtering and transforming text:

sed 's/old/new/g' file.txt

tr — Translate or delete characters:

cat file.txt | tr 'a-z' 'A-Z'

unix2dos — UNIX to DOS text file format converter:

unix2dos file.txt