Questions tagged [variable-substitution]
Use where the name of a variable is textually substituted with the value of the variable
312
questions
0
votes
2
answers
106
views
How to grep for the value of a variable inside a shell script?
I have 2 files:
a file full of values I want to look for
my source text file
I wrote a short shell command to loop thru my list of values and grep against my source file. If it doesn't find the ...
0
votes
3
answers
54
views
How to expand variable inside single or double qoutes
I have 2 systems and they have the same directory strcuture. I want to execute a couple of commands on local system and the same on remote system.
I use the wildcards so when I have a new version of ...
0
votes
1
answer
66
views
${parameter:?word} expansion for array range
${parameter:?word} prints the given error message if a variable is unset:
$ echo ${unset_var?badness}
bash: unset_var: badness
How to use this on an array?
$ array=(a b c)
$ echo "${array[@]:1:2}...
3
votes
1
answer
51
views
${parameter:?word} expansion: `\}` difference between bash and zsh
I'm trying to get a literal } in the output of the ${parameter:?word} expansion in zsh.
In bash, it works as expected -- I can quote the } with a \:
% bash
$ echo "${unset_var_name?...
1
vote
3
answers
77
views
ksh parameter expansion: replace / with -
Say I have this:
str="@test/string"
echo $str
@test/string
echo ${str#@}
test/string
works as expected, but
echo ${str//\//-}
ksh: ${str//\//-}: bad substitution
fails. (expected @test-...
0
votes
1
answer
46
views
Shell script: Using variables makes command fails ( substituting values of variables manually ; command works fine )
In a bash script:
jenkins_folder=`cut -d "|" -f1 -s input.csv`
jenkins_url='https://url.com:8181/jenkins/view/'
echo "jenkins_folder : ${jenkins_folder}"
for job in ...
0
votes
1
answer
73
views
rtorrent scripting help
rtorrent provides a nice script for moving downloads to a directory based on label or name https://rtorrent-docs.readthedocs.io/en/latest/use-cases.html#versatile-move
i'm trying to configure the ...
-3
votes
2
answers
249
views
Bash Script - Expand ESCAPED dollar-sign ($) into its Variable
My Problem
I have this run.sh script:
#!/bin/bash
TODAY=$(date)
FILE="my_file.txt.\${TODAY}"
When I echo FILE I get this:
echo ${FILE}
Output: `my_file.txt.${TODAY}`
But I want this:
...
0
votes
1
answer
63
views
unterminated s command with seed on special characters with sed [duplicate]
i have special characters in the content of $VALUE which I would to add in file ($FILE).
When I run this cammand :
sed -i "s|T_VALUE|$VALUE|" "$FILE"
i have an error unterminated ...
0
votes
0
answers
25
views
Text processing involving shell variables doesn't evaluate the variable correctly [duplicate]
I have special characters in the content of $VALUE which I would to add in file ($FILE).
When I do :
sed -i 's|T_VALUE|$VALUE|g' "$FILE"
my file contains the literal string $VALUE instead ...
2
votes
2
answers
3k
views
Bash variable substitution into yq command
Given a YAML file, example.yaml:
node:
sub_node:
get_this:
I'd like to get a variable containing get_this using Mike Farah's yq and the string sub_node
yaml="$(cat example.yaml)"
...
1
vote
1
answer
1k
views
curl not working with when a parameter is passed via a variable in shell script
The following command works when entered directly on the console:
[root@server user]# curl -d 'parameter=170.22.16.14 192.10.2.24 - - [01/Jan/1900:00:00:00 -0300] "GET /files/notes.txt HTTP/1.1&...
0
votes
0
answers
30
views
awk: show either a specific line or range of lines but through variable(s) [duplicate]
About the awk command to show a range of specific lines the following tutorial is valuable
AWK command in Unix/Linux with examples
See the Another use of NR built-in variables (Display Line From 3 ...
0
votes
1
answer
25
views
Error in defining Variables
I am trying to follow this to replace a multiline text.
But to do that I want to put some text like below into a variable.
```dataview
TABLE WITHOUT ID page-no as no, link(file.name, topic) as name ...
0
votes
3
answers
79
views
why doesn't A=\"\" grep $A behave like grep ""?
I am on Linux, using bash.
$ A=\"\"
$ grep $A
nomatch
""
"" <- (returned by grep)
$ grep ""
imatcheverything
imatcheverything ...
0
votes
3
answers
124
views
What does echo $$ do? [duplicate]
I was messing around with echo and was trying stuff like echo ~. When I was trying to use $ to substitute environment variables I accidentally tried echo $$. Surprisingly instead of an empty line I ...
0
votes
1
answer
58
views
Bash - for range no longer works as wanted when one of the ends is a variable [duplicate]
#!/bin/bash
myfirstarray=(1 3 5 7 9 11)
for i in {2..4}
do
for j in {1..${myfirstarray[$((i-1))]}}
do
echo ${j}
done
done
In the code above the range of the ...
2
votes
1
answer
61
views
mailx bcc cc options null hanlde
I am using below command for sending email, it working only if all parameter supplied.
Please advise how to handle null/blank variable. For example, if -c -b is not supplied then ignore.
mailx -a ${...
0
votes
1
answer
941
views
Prevent splitting command line argument at spaces when running in bash script [duplicate]
I'm trying to run a command in a bash variable, like this:
cmd="python -m mymodule 'argument 123 456' argument2=32 argument3=234"
$cmd
It looks like it is splitting the command line ...
0
votes
0
answers
47
views
How can I use an already defined variable reference in definition of another variable in shell script/ [duplicate]
I am trying to do something like below :
export a="ABC" # Works fine
export b_"$a"="DEF" # Works fine
Now how do I print or echo the new variable "b_$a" ??
I ...
0
votes
1
answer
55
views
bash: different behaviour of script and in terminal (loop over files)
I'm trying to loop over files with having different searching conditions based on the folder which I used with a case statement.
basically it's:
#!/bin/bash
case folder in
"Testordner")
...
0
votes
2
answers
450
views
Export a value from input variable
I need to export the values from input. However it's failing in bash.
Here's the code -
echo "Please enter HOST :"
read RDSHOST && export RDSHOST
And my value is something like - ...
2
votes
2
answers
780
views
How does the tilde expansion work within a shell variable?
I came across something funny when testing my script.
I can ls my directory from the shell manually if I run
$ ls ~/db_backups/
test1 test2
$
However, if I assign a shell variable a dir location as ...
3
votes
4
answers
454
views
Bash function inside function: How to interpolate certain variables from outer function
I'm trying to create a function that creates a function. I want to "hardcode" an argument in the inner function based on what is passed into the outer function, e.g.
outer() {
inner() { ...
0
votes
2
answers
1k
views
Using grep and command line argument
I have a file f1 with contents:
james john joe
marie james
joe
don marie
I want to output the occurrence of the command line arguments in each line using grep in a shell script s1.sh with ...
0
votes
1
answer
122
views
How can I batch rename files by combining three variables and ignore file extensions?
I'm trying to create a script that renames a group of files. Normally they will be TIFs but sometimes an occasional oddball could be thrown in so I would like to rename the file and ignore the file ...
0
votes
2
answers
779
views
sed and variables-substitutions on an custom script
well i ask here because i have around 12 hours trying with examples on internet for put a replace with sed on a custom script.
In the file a had a lots of lines but i want to replace values after &...
-1
votes
1
answer
255
views
How is the match evaluated in parameter expansion in bash?
I read "Linux Bible 10th Edition", chapter 7: Writing Simple Shell Scripts, at 149 page,
"Parameter expansion in bash" paragraph.
${var:-value} — If variable is unset or empty, ...
0
votes
1
answer
66
views
Find with optional parenthesised arguments
How to add optional grouped argument lists to find?
For example, using an array of base arguments that define files that will always be found, no matter what other variable conditions are specified.
# ...
0
votes
2
answers
21
views
Putting variables in cardano-wallet function in terminal
How to enable variables in terminal to be changeable, for example address, I would like to be able to run this function through for loop in terminal with different addresses.
0
votes
2
answers
37
views
When looking at subdirectories, skip one directory
I have a directory /u01/oracle/folders with these subfolders:
[root@ricusesasctl01vm tax_receipts]# ls -ltr
total 64
drwxr-xr-x 5 OICDev1 oic 4096 Mar 1 2021 Argentina
drwxr-xr-x 5 OICDev1 oic 4096 ...
0
votes
0
answers
19
views
Something in the file is messing with ZSH [duplicate]
I have a file with such lines:
map_Kn Ellie_Caruncle_N.png
I'm trying to extract field #2, and use it twice in a command like this, but it's giving me some problems.
Here is a ZSH session:
~/Downloads/...
1
vote
1
answer
49
views
Using Pipes to Generate Variable for Execution
I have the following test case that I am trying to work with:
countAlpha=10;bob=alpha; echo $(echo $bob | sed 's/[a-z]/\U&/; s/^/\$count/') $countAlpha
Which produces the following:
$countAlpha ...
0
votes
0
answers
18
views
cd command to a variable containing a path with spaces gives different output than actually giving the path as an input [duplicate]
I have a directory named "test 1" and I create a variable
dest="test\ 1"
I want to navigate to the directory using the cd command, so I type
cd $dest
which gives the output as
-...
0
votes
0
answers
347
views
how to expand a variable with backslash in bash script [duplicate]
i read a line from another file that contains a backslash, and i want to echo this line without expanding the backslash :
script.sh :
#!/bin/bash
while read line
do
echo "$line"
done &...
0
votes
2
answers
37
views
Can the list if the variable used in a for loop be made up of variables themselves?
I'm trying to create a script where pretty much things got sorted out earlier in it and I'm left with the booleans I need stored in variables. I just need to run them through one last loop and I'm ...
0
votes
1
answer
511
views
passing arguments to a command run by su
I want to run a command as a different user (the current user is root). Thus, I do
su newuser -c 'mkdir "/tmp/test"'
but I have a problem in passing arguments to it
i="name"
su ...
1
vote
1
answer
96
views
How to store '>&2' in a variable
Similar question, but no answer: How can we run a command stored in a variable?
How to do the following in bash script?
error=">&2"
echo 'something went wrong' $error
instead of
echo ...
0
votes
1
answer
637
views
Printing a shell variable yields empty output
I have the following program where I want to print a variable $var that I fill with the execution time of a program. However, when outputting it to the console with echo or printf the value is empty.
...
1
vote
3
answers
2k
views
Bash interpreting a variable assignment as a command
I've been trying to do something for a couple of days, and I'm stumped; I keep running into the same problem, no matter how I approach this. I have a text file with 2 columns in it; the first is the ...
1
vote
2
answers
458
views
Asterisk/Double Quote Failing to Excecute
Sory I'm new to shellscripting. I have a variable that contains an option related to find command.
TYPE=("-type f")
NAME=("-name \"*log*\"")
I try put those variable ...
2
votes
0
answers
75
views
zsh: Make getting the value of a variable invoke a function
I like to use a "getter" function for some variable foo, such that using "$foo" is equivalent to "$(foo-getter)".
It think this should be possible, as $EPOCHREALTIME ...
2
votes
1
answer
2k
views
How to deal with special characters in docker registry credential in new version of kubectl
Today I want to add a docker registry credential in kubernetes v1.21.3 (it works fine in lower version of kubernetes but have problem in new version of kubernetes):
kubectl create secret docker-...
2
votes
2
answers
289
views
Unable to use a variable to specify the targets for rsync's "--exclude={..}" option within script
My goal is to have my bash script run this command:
rsync -azhi --dry-run --exclude={'file1.txt','file2.txt','*.sql'} /from-directory/ /to-directory/
... when abstracted thusly:
srcdir='/from-...
0
votes
3
answers
3k
views
How to split string by spaces, which contains quoted values
Take the following bash example (I should mention that my real output comes from vboxmanage list vms)
TEST='"ubuntu server" foo bar'
echo $TEST
"ubuntu server" foo bar
How do I ...
6
votes
2
answers
2k
views
Keep matching pattern in shell parameter expansion
I can remove a pattern in a bash variable using ${variable##pattern} (leading) or ${variable%%pattern} (trailing).
But I can't find a bash-only way to keep the pattern and throw the rest.
I know there ...
2
votes
2
answers
814
views
awk evaluate variable in statement
I am new to awk, I tried numerous suggestion found online, but I cannot resolve my problem.
I need variable $number to get evaluated inside the -F arg value.
This statement works as expected:
inn=$(ip ...
1
vote
1
answer
424
views
Error while executing "sed" command that uses shell variables
This has been driving me crazy...
I want to list files in a dir and, based on the number interate on all files except the latest one, here is the code snipplet
FILECOUNT=$(ls -lt [dir]*.log | grep -c ...
3
votes
1
answer
861
views
Bash Script : Problem with variables from csv file
I'm having a little problem getting variables from a csv file on my Linux machine and using them in an if. I have the following csv:
Name;Age
Marc;18
Joseph;10
I'm trying to get this information from ...
3
votes
1
answer
4k
views
Grep pattern that has double quotes and variable substitution surrounded by single quotes
So, Let's say I have the following pattern:
Thisisatest="1"
in a file called file.
And I want to match the exact string above but for whatever reason, I choose to loop over some numbers ...