Questions tagged [variable-substitution]
Use where the name of a variable is textually substituted with the value of the variable
340 questions
12
votes
2
answers
954
views
Why arithmetic syntax error in bash causes exit from the function?
I was debugging one script, and discovered unexpected behavour of handling arithmetic syntax error.
Normally, when error happens, script is just continuing execution.
#!/bin/bash
func2 () {
echo &...
4
votes
2
answers
339
views
Deferred variable expansion including in a subshell
I have a command that takes always the same initial parameters. I can create a variable to capture these parameters. Also, I need to pass the output of a subshell running the same command to the ...
2
votes
1
answer
289
views
How to get multiple pattern rule %/$* variables in a Makefile?
All we get in Makefiles is just one %/$* pattern rule pair.
$ cat Makefile
%.bla:; echo $*
$ make -s m.bla
m
How shortsighted of our Unix™ fathers.
How can I achieve something like %{0}.%{1}.bla
that ...
0
votes
0
answers
45
views
bash variable substitution inside heredoc delimiter: how does it work? [duplicate]
For a school project, I am tasked with making my own (simplified) shell, with bash being the reference point. This includes replicating heredoc behavior which was fun until I stumbled upon variable ...
6
votes
4
answers
908
views
Using curly braces to process colon-separated 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
75
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
127
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
59
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
339
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
249
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
135
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
468
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
1
answer
52
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
82
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 $...