Questions tagged [variable-substitution]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
3
votes
1answer
813 views

Double Quotes in Bash's variable substitution

I am trying to configure some software packages with a script thereby I got the following problem. Assume that the environment variable PREFIX is set to the location where I plan to install the ...
4
votes
2answers
1k views

How does Bash handle quoting in the string replacement section of parameter expansion?

Is there any consistent logic to it? some-command "${somevariable//some pattern/'how does this get parsed?'}" I've posted some conclusions and raw tests below as an "answer" but they're not a full ...
1
vote
1answer
435 views

Run cd with path variable in bash script not working [duplicate]

When i run the script: #!/bin/bash DRUPAL_ROOT=$(drush status root --format=list) if [ -z $DRUPAL_ROOT ] then echo -e "Not exists Drupal core" else echo $DRUPAL_ROOT cd $DRUPAL_ROOT fi Output: ...
-1
votes
1answer
721 views

Variable won't get expanded when INSIDE mysql shell

Before creating a new mysql user and DB, I did: read sps -s # Inputing and saving the sps (password) value. echo ${sps} # sps password value echoed. mysql -u root -p[PASSWORD] mysql>...
1
vote
3answers
94 views

How to dynamically create Bash code with a loop and execute it as it is created

I have a list of lines in a Bash script as follows if [ ! -z "$clone01" ]; then git clone "$clone01"; fi if [ ! -z "$clone02" ]; then git clone "$clone02"; fi if [ ! -z "$clone03" ]; then git clone "$...
2
votes
1answer
1k views

How can I change a Bash variable name in a loop and then expand the changed name?

I'm trying to expand variables Bash. I've searched but am not sure what the search terms are for what I am trying to do. Here is my code. Two questions: How do I get the desired output as shown? ...
7
votes
1answer
625 views

Word splitting in positional parameters

Consider the following example: IFS=: x="a :b" # three spaces echo ["$x"] # no word splitting # [a :b] # as is echo [$x] # word splitting # [a b] # four spaces Word splitting ...
0
votes
2answers
60 views

Reading output of command into variable in shell, splits text at every white space not just newline

I'm using sh (not bash/csh) on FreeBSD 11, and I don't understand this: In console Command: zpool status -v Result: pool: My_pool state: ONLINE status: One or more devices is currently being ...
2
votes
2answers
292 views

Wildcard not being interpreted in if statement

I am currently writing the below script. The code looks at a certain directory for a filename inputted by the user. The script first checks to see if the input file is a gzip, if so, it runs the ...
2
votes
3answers
195 views

Variable Substitution

I have a variable assigned to a returned string: ytd_wk=$(cat file.csv | grep $(date +'%Y') | tail -1) I want to substring last 2 characters: ytd_wk=${ytd_wk:(-2)} Is there any way to use one-...
0
votes
3answers
324 views

Test nested variables in a while loop

I was trying to insert a nested variable as a while loop condition but I can't manage to expand it properly. print_message() { timer=0 timer_condition="$2" while [[ $timer_condition ]]; ...
0
votes
1answer
62 views

Why is variable substitution called variable substitution if we retrieve the value?

I've read here the following text: The name of a variable is a placeholder for its value, the data it holds. Referencing (retrieving) its value is called variable substitution. If we retrieve/...
6
votes
3answers
516 views

Why is there a number in the zsh parameter expansion ${1-$PWD}

I have this script I'm basing my current script off of. I just don't understand why he has typeset result part dir=${1-$PWD} in there. I get the same result if I just write dir=$PWD. With typeset is $...
2
votes
0answers
365 views

Replacing *any string* dynamically with POSIX shell [duplicate]

I'm developing a POSIX compliant script in which I want to do a dynamic replace of strings, something like: sed "s/${pattern}/${replace}/g" Problem: if the pattern includes the delimiting character, ...
0
votes
2answers
503 views

what does it mean shell read command line arguments ${1,,}

In the Shell script code, the command line arguments assigned to variable like below. what does it mean comma(,) in the statement. What will be the difference when comma is added twice while reading ...
-1
votes
1answer
54 views

how generate and substitute files with sed [closed]

I need to change the seed's value, the output (HA.xyz) and the namefile to generate 100 different files. # MSA: H2O seed 129472 tolerance 1.0 filetype xyz output HA.xyz Structure H2O.xyz number ...
1
vote
2answers
1k views

Behaviour of bash command substitution with command from string in variable

$ echo $(echo x; echo y) x y $ a='echo x; echo y' $ echo $($a) # expect 'x y' x; echo y Why does command substitution behave in such way? How to perform the command substitution for list of commands ...
1
vote
1answer
107 views

Passing paths and filenames to a program from bash script

I use a utility called mp3wrap which combines multiple mp3s into one. Say I have a directory "album 1" containing three files "track 1.mp3", "track 2.mp3" and "track 3.mp3". I will run the following: ...
13
votes
4answers
32k views

Escape a variable for use as content of another script

This question is not about how to write a properly escaped string literal. I couldn't find any related question that isn't about how to escape variables for direct consumption within a script or by ...
0
votes
3answers
185 views

Confusion about bash command vs. variable substitution

I've been writing one-line bash scripts on the command line since 1989. These usually have the form: for name in 1 2 3; do wc $name.txt; done Now I'm trying to do some image manipulation with GIMP, ...
3
votes
3answers
260 views

bash - extglob 2nd “zero or more” operator not working

Given: $ shopt -s extglob $ TEST=" z abcdefg";echo ">>${TEST#*( )z*( )}<<" >> abcdefg<< Why is there a space before the letter 'a'? I would expect that the 2nd *( ) would ...
2
votes
4answers
425 views

I want echo the value in a1,a2,a3. But my code is printing only a1,a2 and a3 not the value stored inside them

a1="one" a2="two" a3="three" for ((i=1;i<=3;i=i+1)); do echo $a$i done I have already tried following echo ${a$i} echo $((ai)) echo ${a}${i} c=$(echo a$i) But none gave the right answer.
4
votes
2answers
144 views

Shell: why [ -n “${VAR-}” ] instead of [ -n “$VAR” ]?

I see a lot of shell scripts (for example, this one) checking for a variable's presence/absence like: [ -n "${VAR-}" ] As far as I can tell, using the ${VAR-fallback} form without providing a ...
-1
votes
1answer
510 views

Why is there a “Variable substitution” concept in Bash?

This question is very basic and I ask it not only for myself but for more newcomers who see the term "Variable substitution" and having the following thoughts: As far as I understand, the term ...
3
votes
1answer
928 views

error: Macro % has illegal name (%define)

I have a build support script for building my packages. Now I want to automatically set the Version in my spec files. I use the macro %{auto_version} for that. In my script I want to invoke rpmbuild -...
0
votes
2answers
86 views

How to leverage a user defined variable in a command to create a condition in an if [duplicate]

I have a script which takes input for two variables from users. Then, I try to use those variables in a command which is in turn being fed into a condition in an if. However, the user input value is ...
0
votes
1answer
446 views

Redirect the output of a command with variables to an array

I am trying to redirect the output of a command that contains couple of user input variables to an array. I first tried this script - echo "Type the ACL name" read acl echo "Type the DATACENTER name" ...
14
votes
3answers
3k views

Does bash support back references in parameter expansion?

I have a variable named descr which can contain a string Blah: -> r1-ae0-2 / [123], -> s7-Gi0-0-1:1-US / Foo, etc. I want to get the -> r1-ae0-2, -> s7-Gi0-0-1:1-US part from the string. ...
1
vote
2answers
543 views

Use sed command with variables

I have tried every possible solution available on the Stack Overflow and its related websites but did not find any solution. I spent a reasonable amount of time on this issue and am finally posting ...
11
votes
2answers
5k views

Bash variable substitution of variable followed by underscore

The variable BUILDNUMBER is set to value 230. I expect 230_ to be printed for the command echo $BUILDNUMBER_ but the output is empty as shown below. # echo $BUILDNUMBER_ # echo $BUILDNUMBER 230
0
votes
1answer
962 views

Bash variable substitution with spaces [duplicate]

There's next code: TITLE="Some value with spaces" DIALOG="${DIALOG=dialog} --clear --no-tags --tab-correct --backtitle $TITLE " ... $DIALOG --title "Some title --menu "Menu" 15 60 5 "1" "menu1" "2" "...
12
votes
3answers
1k views

Is “${PS1-}” valid syntax and how does it differ from plain “$PS1”?

I am looking at a script that has: if [ "${PS1-}" ]; then That trailing - bugs me a bit because it doesn't seem to Posix or Bash standard syntax. It this some arcane syntax that has been around ...
2
votes
3answers
70 views

Adding in basic variable command

Commands are.. t=20 s=$t+30 I want the output of echo $s to be 50, but I cannot figure out how to write the s= command to get it to perform addition.
5
votes
3answers
4k views

Spaces for variables in bash script

I have a space in one of the directory names. I want to list a file under it from a bash script. Here is my script: fpath="${HOME}/\"New Folder\"/foobar.txt" echo "fpath=(${fpath})" #fpath="${HOME}/$...
3
votes
1answer
302 views

Exim condition failure “${env is not a known operator”

I am trying to substitute a hard-coded path in my Exim configuration file with a value taken from an environment variable, however I am getting an error I do not understand. "${env" is not a known ...
1
vote
1answer
213 views

How to retrieve a part of a path name and assign it to a variable?

I want to assign a part of my current working directory path name to a variable and use it in a script inside the directory itself. For eg: If my pwd is : /home/desktop/project/ABC/abc/abc_123, is ...
4
votes
1answer
556 views

readlink literal vs variable

Using BASH: the answer is probably obvious but, not for me. > echo $PWD /root/fcm > readlink -f ~ /root > # but then with a variable or literal > a='~' > readlink -f $a /root/fcm/~ >...
3
votes
2answers
92 views

Variable from concatentation of variables [duplicate]

I think this is a rather simple question but, I cant make this work: I have a whole lists of variables eg: SP60=OLA SP61=BYE SP62=TRT I want to create a loop to call them in the specific number of ...
1
vote
2answers
204 views

sed /PATTERN/SUB/ $VAR?

I am renaming some files. This works: ls | while IFS= read -r line; do name=$(echo $line | sed -e 's/\(.*\)/\1.jpg/') && mv $line $name; done Which is okay but I'd like to make it more ...
1
vote
2answers
62 views

properly using the $ atom in a parameter substitution

I'm trying to perform a parameter substitution that strips everything from the first - char to the end of a string like so: v0.1-bla-hblah-232 -> v0.1 So I'm using the following script: #!/usr/...
0
votes
2answers
620 views

Assign a variable to each line of the output and loop through it in bash

I have a command that will have multiple lines as output. i will need to use each line as a source to another Command to process it. i am trying to assign a variable and loop through it, but it fails ...
1
vote
2answers
2k views

One line env variable set behavior [duplicate]

If I do this: SUMAN_DEBUG=foo echo $SUMAN_DEBUG I get no output but if I do: SUMAN_DEBUG=foo && echo $SUMAN_DEBUG then I get "foo" why is that?
0
votes
1answer
58 views

How to create variable from piped data

The command I using is: ls -l . | totLines=$(wc -l) echo $totLines . My goal is display the total number of lines, but by assigning the output of wc -l a variable name, then displaying that variable's ...
3
votes
1answer
663 views

Bash - Parameter expansion for variable with leading white space

I keep wondering about strange rules of parameter expansion in shell. If I declare NUMBERS=" one two " and if execute the following (note that there is no space between variable and the ...
7
votes
2answers
564 views

Any way to show each step during the command processing?

set -x just show a final result of the command. Any way to view each step more clear, like pics below.
1
vote
3answers
64 views

Use nested var for substitute var

I've a big question today ! I've made a little exemple of my situation. FOO="foo"; fooBAR="final"; var=${${FOO}BAR} echo $var I want to print "final". I tried all sort of things like ${${!FOO@}BAR} ...
1
vote
1answer
83 views

Declare as local var will break a function and log out “1: number expected”

function projectopen { local di_files=(*.xcworkspace */*.xcworkspace *.xcodeproj */*.xcodeproj) # open first exsit file ls -d -f -1 $di_files 2>/dev/null \ | head -1 \ | xargs ...
1
vote
3answers
72 views

Use a list file to edit a master file through awk

everyone. I have two files: ports.lst and master.tbl ports.lst looks like this: hawaii-P1 hawaii-P2 hawaii-P3 losangeles-P1 losangeles-P3 master.tbl looks like this: #Site 1 Honolulu servername ...
1
vote
2answers
194 views

Why a value of a variable doesn't change inside backticks?

Here's my test code: a=1 echo $a echo `let ++a` echo $a The output that I see is 1, 1. Why doesn't the third line modify the value of a?
0
votes
2answers
3k views

How do I call a script with a wildcard pattern and two more arguments?

I have this code to rename a bunch of files: #!/bin/bash for file in $1 do mv -i "${file}" "${file/pattern/replacement}" done How do I replace "pattern" and "replacement" with $2 and ...