Bash scripting: When to use variable, when function?
0
votes
2
answers
80
views
basic, innocent question:
In bash scripting, why ever using a function, if one can set a variable containing command substitution with the essence of the function - a certain command or set of commands, which is supposed to output a certain value?
In other words:
Does it matter, if one defines a variable, or a function for a certain, desired output?
When and why to implement it as a variable? When and why it's better to implement
it as a function?
Example:
Let's say, there is a directory on your system, which contains a lot of sub-directories in 1st level, and you want to find out with a bash script, what's the most recently modified.
In a bash script, you can define a variable
for it, and
print out its content on demand:
#!/bin/bash
# latest-directory-displayer
#
# Copyleft 🄯 2024
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see .
# Displays what's the most recently modified sub-directory within
# current directory.
# Tool variable set
find="/usr/bin/find"
sort="/usr/bin/sort"
tail="/usr/bin/tail"
grep="/bin/grep"
sed="/bin/sed"
# Variable set
rece_dir="`"$find" . -maxdepth 1 -type d -printf '%T+ %p\n' | \
"$sort" | "$tail" -1 | "$grep" -o "/.*" | \
"$sed" 's/ /\\\&/g;s+$+/+'`"
# Function set
################################## Main part ###################################
printf "$rece_dir\n"
exit
Cute. But why not doing it like this?
#!/bin/bash
# latest-directory-displayer
#
# Copyleft 🄯 2024
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see .
# Displays what's the most recently modified sub-directory within
# current directory.
# Tool variable set
find="/usr/bin/find"
sort="/usr/bin/sort"
tail="/usr/bin/tail"
grep="/bin/grep"
sed="/bin/sed"
# Variable set
# Function set
display_latest_dir() {
"$find" ./ -maxdepth 1 -type d -printf '%T+ %p\n' |
"$sort" |
"$tail" -1 |
"$grep" -o "/.*" |
"$sed" 's/ /\\&/g;s+$+/+'
}
################################## Main part ###################################
display_latest_dir
exit
Same output, same basic inner workings, but one over variable, while the other over function.
A minor difference I've spotted was different escape requirements.
As soon as you put your set of commands into a variable as command substitution,
it probably needs 1x more \
wherever you've escaped with a back slash.
Why not always use variables, instead of functions? Why not always use functions, instead of variables?
Asked by futurewave
(213 rep)
Jun 7, 2024, 05:29 PM
Last activity: Jun 7, 2024, 07:07 PM
Last activity: Jun 7, 2024, 07:07 PM