BASH: Global variables aren't updateable in a function only when that function is piped (simple example)
Date : March 29 2020, 07:55 AM
|
bash: call function with variables vs function with arguments
Tag : bash , By : Hugo Hernan Buitrago
Date : March 29 2020, 07:55 AM
Hope that helps The question has nothing to do with Bash as such. The #1 is the example of "Spaghetti" coding style (global variables) hated by most professionals and simply sane people. It will eventually cause a major problem when someone somewhere changes the parameter and the function starts misbehaving, and you won't have a clue of who/what has changed what where.
|
How to install a bash function containing variables using a bash script?
Tag : bash , By : Justin Bowers
Date : March 29 2020, 07:55 AM
this will help A here document is treated as a double-quoted string, so parameter expansions and command substitutions are evaluated before the command reads from them. Quote any part of the delimiter to have the here document treated as a single-quoted string. cat <<\EOT >> ~/.bashrc
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# create a file backup in ~/filebackup/ with timestamp
filebackup () { cp "${@}" ~/"filebackup/${@}_$(date +%Y-%m-%d_%H:%M:%S).bk"; }
EOT
|
Bash lost variables after function
Tag : bash , By : CookingCoder
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , The shell runs whatever present under (..) in a sub-shell and especially variables the defined lose their scope once the shell terminates. You needed to enclose the function within {..} which encloses a compound statement in bash shell to make sure the commands within are run in the same shell as the one invoked. function askExp { read -ep "$1" -n "$2" -r "$3"; }
hw()(
echo hello world from $BASHPID
)
hw
echo $BASHPID
hw(){
echo hello world from $BASHPID
}
hw
echo $BASHPID
|
how to call a bash function providing environment variables stored in a Bash array?
Date : March 29 2020, 07:55 AM
will be helpful for those in need I got two variables in a bash script. One contains the name of a function within the script while the other one is an array containing KEY=VALUE or KEY='VALUE WITH SPACES' pairs. They are the result of parsing a specific file, and I can't change this. , You can do like this: declare -a funcenv=(MYVAR=test "MYVAR2='test2 test3'")
for pairs in "${funcenv[@]}"; do
eval "$pairs"
done
"$funcname"
|