Advanced Bash Scripting: Part 2 🔥

Become a Bash Scripting Ninja 🥷!

Alex Rodriguez
4 min readFeb 25, 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 of this series, go check it out here, por favor ❤️!

For-loops Without “in [list]” Defined

# you don't need to define a list to loop over when defining a for loop!
# `arg` takes on the value of the current positional parameter!
for arg; do
echo $arg
done
# ./script.sh 1 2 3
# 1
# 2
# 3

# so it can be applied in functions too!
func_expecting_args()
{
for arg; do
echo "parsing arg: $arg"
done
}

func_expecting_args Alex 22 'Security Engineer'


# Bash is smart ...

Using For-Loop to Iterate Over Output of Function

get_config()
{
echo 'NumOfThreads 20'
echo 'MaxRequests 100'
echo 'Timeout 10'
}

IFS=$'\n'
for config in $(get_config); do
IFS=' '
set -- $config
config_name=$1
config_value=$2
echo "Setting $config_name to $config_value ..."
done

# ./script.sh
# Setting NumOfThreads to 20 ...
# Setting MaxRequests to…

--

--

Alex Rodriguez

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