Notes by Peter Galonza(Пётр Галонза)
GitHub Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Toggle Dark/Light/Auto mode Back to homepage

Bash

EOF

< dev/null

Heredoc

cat << EOF
  <text> $PWD
EOF
cat > file_name << EOF
  <text> $PWD
EOF
cat << EOF >> file_name
<text>
EOF

PID of last process

echo $!

Rename file

mv /tmp/working-dir/readme.md{,.backup}
rename <>

Don’t save commands of the current session

unset HISTFILE
export HISTFILE=/dev/null
export HISTSIZE=0
set +o history
kill -9 $$

Clear command history

history -cw
rm ~/.bash_history
history -r

Show last command execution result

echo $?

Show last process pid

echo $!

Executing command for each row

cat file_name.txt | while read in; do <command_line> "$in"; done

Sequential execution of commands

<command_1>; <command_2>; <command_3>

Parallel execution of commands

(<command_1> &); (<command_2> &)

Output one command as an argument to another

<command_1> $(<command_2>)

Execute the command without history.

space <command>

Automatic answer

yes/no | <command>

Сommand replay

watch <command>

Execute the command in another directory and return

(cd /tmp && ls)

Run the command at the specified time

echo "ls -l" | at midnight

Evaluate contents of file

source <file_name>
. <file_name>

Replace

${<source text>/<pettern>/<replacement>}

Run in a single instance

[ "$(pidof -x $(basename $0))" != $$ ] && exit
/usr/bin/flock -w 0 /tmp/test.lock -c '<file name or command>' || echo "cannot be executed an instance already runs"

Error if variable not exist

rm -rf /${dirname:?}

If in one line

if [[ <condition> ]]; then <command>; else <command>; fi
[[ <condition> ]] && <command>
test <condition> && <command>

For in one line

for <variable> in <list>; do <command>; done

No finish if error

<command> || true

Debug

Information from

#!/bin/bash

trap 'echo "# $BASH_COMMAND";read' DEBUG

Read password in variable

read -s <variable name>

End of argument processing for command

<command> -- <command> <args>

Deactivate alias

Information from

/<command>
'<command>'
"<command>"
<path to binary file>
unalias <alias>
unalias -a
command <command>

Вetaching a process from a session

Information from

Ctrl + Z
bg <pid>
disown <pid>

Cron sctipt template

Variables

Separator

IFS=$"<value>"

Information from

  • PS0 - value of this parameter is expanded like PS1 and displayed by interactive shells after reading a command and before the command is executed.
  • PS1 - primary prompt string.
  • PS2 - secondary prompt string.

File descriptors

Open descriptor.

  • write
  • read
  • read and write
exec <fd>> <file>
exec <fd>< <file>
exec <fd><> <file>

Close descriptor

exec <fd>>&-
exec <fd><&-

Merge descriptor

exec <fd> >&<fd>
exec <fd> <&<fd>

Move descriptor

exec <fd><&<fd>-

Read descriptor

read input <& <fd> && echo "${input}"

Open socket

exec <fd><>//dev/<protocol>/<ip>/<port number>

I/O redirect

Stderr and stdout in file

<command> > <file> 2>&1
<command> &> <file>

Stderr in stdout of console, stdout in file

<command> 2>&1 > <file>

Pipe

mkfifo <pipe name>
echo "Text" > <pipe name>
cat < <pipe name>

Process Substitution

cat < (echo "Text" >)

Prompt

Label for warn the criticality of the server

PS1="\@\[\033[38;5;42m\]<server type>\[$(tput sgr0)\]\n[\[$(tput sgr0)\]\[\033[38;5;75m\]\u\[$(tput sgr0)\]@\[$(tput sgr0)\]\[\033[38;5;42m\]\h\[$(tput sgr0)\] \W]\\$ \[$(tput sgr0)\]"

Bash options

Show bash options

echo $-

Arguments in enviroments

set -k
command <argument>=<value>

Does not execute commands

set -o noexec