I want to pass a string parameter to a Bash procedure. This procedure prints the string on console and prints a copy to a file.
In my use case, this file will contain a list of all executed commands in a Bash script that can be used to rerun all instructions in the same order. This is needed if an error occurs and I need to send a reproducer script to an open sourc project on GitHub. It will also copy all used files into a directory for later ZIP file creation.
So, let's talk Bash code:
#! /bin/bash
open() {
local File=$1
exec 3<> "$File"
}
close() {
exec 3>&-
}
procedure1() {
echo "$1"
echo "echo \"$1\"" >&3
}
procedure2() {
echo "$1" "$2"
echo "echo \"$1\" \"$2\"" >&3
}
procedure3() {
echo "$@"
echo "echo \"$@\"" >&3
}
# ==============================================================================
OUTPUT_FILE="output.sh"
Program_A="foo"
Paramater_A=(
--spam
--egg=4
)
Program_B="bar"
Paramater_B=(
--ham
--spice=4
)
open $OUTPUT_FILE
echo "$Program_A -a ${Paramater_A[@]}"
echo "$Program_B -b ${Paramater_B[@]}"
echo
procedure1 "$Program_A -a ${Paramater_A[@]}" "$Program_B -b ${Paramater_B[@]}"
procedure2 "$Program_A -a ${Paramater_A[@]}" "$Program_B -b ${Paramater_B[@]}"
procedure3 "$Program_A -a ${Paramater_A[@]}" "$Program_B -b ${Paramater_B[@]}"
close
echo
echo -e "\e[33m========================================\e[0m"
echo -e "\e[33mReading output file from disk\e[0m"
echo -e "\e[33m========================================\e[0m"
echo
cat $OUTPUT_FILE
The console output is this:
$ ./test.sh
foo -a --spam --egg=4
bar -b --ham --spice=4
foo -a --spam
foo -a --spam --egg=4
foo -a --spam --egg=4 bar -b --ham --spice=4
========================================
Reading output file from disk
========================================
echo "foo -a --spam"
echo "foo -a --spam" "--egg=4"
echo "foo -a --spam --egg=4 bar -b --ham --spice=4"
So what I see is, that ".... ${Parameter_A[@]} ..."
is contained in a string, but breaks the string into multiple strings. That's Why $1
in the procedure contains the string including the first parameter value.
How to embed all parameters into a single string without breaking it up into multiple strings?
$@
works to print all texts, because it contains all parameters passed to the procedure. However, it's not a solutions to me, because I can not distinguish when the string from $2
starts or in other words, how many parts belong to $1
.