Advanced Bash Scripting: Part 3🔥

Become a Bash Scripting Ninja 🥷!

Alex Rodriguez
4 min readFeb 29, 2024
source: https://bashlogo.com/

Hello, World! Do you use command-line frequently? Or find yourself writing scripting Bash often? If your answer is yes, then this blog (more like cheat sheet!) is just what you were looking for because I’ll be providing you with some cool tricks that I’ve learned while diving deeper into the Bash scripting language. Let’s go for it!

ATTENTION: If you haven’t read part 1 or part 2 of this series, go do that now ! Por favor! ❤️

Setting Variables Using Command Substitution

# set variable to content of file
data=$(< file.txt)
# same as `data=$(cat file.txt)`

# set `range` variable to output of for-loop
range=$(for i in seq 1..9; do
echo -n "$i"
done)
# 123456789

# read from STDIN and store input directly in variable
name=$(read -p 'What is your name?: ')
age=$(read -p 'How old are you?: ')

# And of course, you could have nested command substitution!
md5_file_hash=$(md5sum <<< $(<file.txt) | awk '{print $1}')

Special Variables

# REPLY holds the value read from STDIN if no
# variable was provided to `read` command
read -p 'Enter your name: '
echo $REPLY

# TMOUT holds the default timeout (in seconds) for the `read` command.
# Once the timeout is reached, the command exits.
export TMOUT=10
read -p 'You…

--

--

Alex Rodriguez

I am an Offensive Security Engineer @ Amazon who writes about cybersecurity and anything related to technology. Opinions are my own.