Questions tagged [variable-substitution]
Use where the name of a variable is textually substituted with the value of the variable
339 questions
3
votes
4
answers
163
views
Using curlybraces to process colon seperated variables
if we do:
VAR=100:200:300:400
We can do:
echo ${VAR%%:*}
100
and
echo ${VAR##*:}
400
Are there any equivalents I could use to get the values of 200 and 300?
For instance a way to get only what's ...
0
votes
0
answers
20
views
Shell expension in variable allocation containing the char '*' [duplicate]
For the shell fans (bash) :
I want a variable to get a value resulting from an operation output which contains an globbing character (such as *), and the shell expansion always happens, even if I ...
0
votes
1
answer
49
views
Variable substituion ":+" in automake
On ubuntu 24.04
GNU bash, version 5.2.21(1)-release (x86_64-pc-linux-gnu)
automake (GNU automake) 1.16.5
autoconf (GNU Autoconf) 2.71
From automake doc
${var:+value}
Old BSD shells, including the ...
0
votes
2
answers
83
views
zsh: substitute-if-undef-or-null for $1, $2 …: ${1:substitution} doesn't work
I'd like to have standard parameters, i.e., my minimum reproducible example is:
#!/usr/bin/zsh
a=${1:a}
printf 'a: "%s"\n' "${a}"
b=${2:./build}
printf 'b: "%s"\n' "$...
1
vote
1
answer
46
views
How To Use Quotes In Variable In Bash [duplicate]
I need to assign a command to a variable and execute it with variable in linux bash. The command executes fine from command line but not from the variable because of multiple quotes. I hope backslash ...
1
vote
6
answers
232
views
Move a lot of folders with spaces in the name
I have a lot of webpages saved in my Download folder and I want to move all html files together with its folder (for every "NAME.html" exists "NAME_files/" folder).
The big ...
0
votes
4
answers
172
views
bash - slice quoted substrings delimited by spaces into array
following example:
string=" 'f o o' 'f oo' 'fo o' "
array=($string)
echo "${array[0]}"
outputs:
'f
while the expected output is:
'f o o'
The only solution I came with is by ...
0
votes
1
answer
94
views
Is there a tool that can print out a bash script with the variables substituted? [duplicate]
Say I have a bash script that takes some environment variables and parameters and substitutes those variables in a script.
Is there a tool that can show how the script with the substituted variables ...
2
votes
2
answers
269
views
Zsh Expanding Variables into Arrays or Lists
I looked through some of the posted threads and none of them cover my query.
I have a simple line of code below which prints all the ASCII characters:
echo {' '..'~'}
I want to be able to use a ...
0
votes
0
answers
24
views
Bash variable losing value [duplicate]
I recently added a variable to a script process that will be used as a replacement value via sed for an output script (not bash). This is one of many variables set within an if statement and (for ...
0
votes
1
answer
40
views
Does Bash have an option to diagnose "expanded to empty value" variables? [duplicate]
Does Bash have an option to diagnose (and optionally abort execution) "expanded to empty value" variables?
Example (hypothetical):
$ bash -c 'echo $x' --xxx
bash: line 1: variable 'x' ...
0
votes
1
answer
66
views
Bash nested variable [duplicate]
I source these variables from a txt file in CentOS bash shell:
NAME01=dns1
HOST01=1.1.1.1
NAME02=dns2
HOST02=1.1.2.2
NAME03=dns3
HOST03=1.1.2.3
## many more lines of similar
NUM=02
How can I use $...
0
votes
0
answers
19
views
bash variable problem [duplicate]
Apologies if this is nooby question but I am new to bash scripting and I couldn't find anything similar.
I am trying to use ffmpeg to re-encode a load of files and that requires I pull out some ...
7
votes
1
answer
1k
views
printf - store formatted string output in a variable
Instead of the following
printf '%s\t%s\t%s\t%s\n' 'index' 'podcast' 'website' 'YouTube'
I want to store the printf output in a Results variable, how can I do this?
-1
votes
1
answer
65
views
Output of loop variable shows different value than expected
Imagine I have two folders in my current working directory: back/ and front/. I am trying to enter inside each of them and do some stuff. From the simple script below:
for dir in */ ; do
echo &...