A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.

Questions tagged [variable-substitution]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
4
votes
4answers
4k views

Can I reload variable in a watch command?

Part of my job involves some data handling. One of the tasks is to 'flatten' a set of directories (which we'll call Dir for now), and copy them to a new location called DirFlat. This can take a long ...
4
votes
1answer
602 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/~ >...
4
votes
2answers
2k views

Bash parameter expansion - greedy versus non-greedy

It's a bit of a contrived example but here goes: Say I have a variable 1.2.3.4 containing version information and need to replace the .4 at the end with .5 version=1.2.3.4 echo ${version%.*}.5 1.2.3....
4
votes
4answers
6k views

Substitute placeholders in template

Say I have a shell configuration file config like this: HOST=localhost PORT=8080 Now I have a template template like this: The host is <%= @HOST %> The port is <%= @PORT %> How do I ...
4
votes
1answer
226 views

Different behavior of $() and `` [duplicate]

% PATH="MYPATH" % VAR="PATH" % echo $(eval echo \$$VAR) MYPATH % echo `eval echo \$$VAR` 5707VAR ^^ This is the process number. I thought those two were exactly the same, but obviously there are ...
4
votes
2answers
167 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 ...
4
votes
2answers
2k views

Double variable substitution in bash

Program snippet: BASH_MIN_REQ="2.05" BINUTILS_MIN_REQ="2.12" BISON_MIN_REQ="1.875" BASH_CURR=$(bash --version | head -n1 | cut -d"(" -f1 | cut -d" " -f4) BINUTILS_CURR=$(ld --version | head -n1 | ...
4
votes
1answer
84 views

How to make sure the $var variable in expanded in “$varsomething”?

I'm trying to run something like this for n in 1 2 3 4 5 6 7 do run_this_command whatever.$nx$n.in done The files are named as whatever.1x1.in, whatever.2x2.in, whatever.3x3.in,...
3
votes
1answer
2k views

Is `echo $TEST` expanding an asterisk in the variable a bug? [duplicate]

Is this a Bash bug? $ mkdir test && cd test && echo "a" > "some.file" test$ echo '*' * test$ TEST=$(echo '*') test$ echo $TEST some.file Why is the second output the resolution of ...
3
votes
3answers
297 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 ...
3
votes
4answers
324 views

Assigning command to variable without alias

I just accidentally found out that in bash, we can assign a command into a variable without using alias. g=date $g Mon Jun 27 13:00:40 MYT 2016 That works. Here's another example: jj="ping yahoo....
3
votes
2answers
995 views

Why does $var give the value of $0?

I have written a very simple awk script that reverses every word of a file-: awk ' { for (i=1;i<=NF;i++) { x="" for(j=length($i);j>0;j--) x=x substr($0,j,1); print x }}' file1 The contents of ...
3
votes
2answers
307 views

Bash parameter substituiton within commands

I have a passing understanding of Parameter Substitution, including substrings like so foo="Hello World"; greeting=${foo:0:6} But do I do this with commands? greeting="Hello" md5greeting=$(echo $...
3
votes
4answers
2k views

pure shell complex substitution in variable

Is there any way to substitute text in variables on several patterns at time or even using back reference? For example, I have FILE=filename.ext and I want to change it to filename_sometext.ext. But ...
3
votes
4answers
995 views

awk - replace number greater than 17 digits in a column with -

I have a CSV file containing timestamp values in UTC which I need to replace with -. There may be more than one timestamp in the same column, can you please let me know how do I do that? For example, ...
3
votes
2answers
2k views

Default assignment to a null variable with command substitution

I'm trying to use the syntax: A=${B:-C} where A is the variable, B is the value I'm trying to assign, C is the default value if B is null. Now I want to replace B with the command nc -l 443, so if ...
3
votes
2answers
3k views

Trouble with mv and adding the date

I want to be able to move $oldfile to my backup folder and add the date to the file name. So I tried this... mv $oldfile /home/u0146121/backupfiles/$oldfile_$(date +%F-%T) This just gives me this ...
3
votes
2answers
93 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 ...
3
votes
2answers
3k views

Print file name extension using -exec in find

I am playing around with -exec flag of find command. I am trying to use the flag to print the extension name of files, using a fairly new Linux distribution release. Starting simple, this works: ...
3
votes
3answers
2k views

Variable expansion in Bash

I tried the following commands variable='one|name' echo $variable The output is one|name whereas echo one|name gives an error No command 'name' found. This is reasonable because bash treats | as a ...
3
votes
1answer
64 views

Zsh parameter expansion

I tried to display the content of the variable fpath in column form using parameter expansion, and I couldn't do it. After some trying and web browsing I just found another solution; which does ...
3
votes
1answer
2k 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? ...
3
votes
1answer
777 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 ...
3
votes
1answer
805 views

Running a constructed command from bash script

I constructed a command like this in a bash script a="my_cmd"; a="$a --verbose"; echo $a; $a; It works and executes my command correctly. But when I add an environment variable to the mix it breaks. ...
3
votes
1answer
15k views

Adding a time stamp when moving a file in bash [duplicate]

#!/bin/bash while read server <&3; do #read server names into the while loop if [[ ! $server =~ [^[:space:]] ]] ; then #empty line exception continue fi echo "Connecting to - $...
3
votes
1answer
3k views

variable expansion within command substitution over SSH bash 4.X

I want to do this ssh ${w100user}@web100 'ls -l "$(grep "${1}" /etc/pure-ftpd/pureftpd.passwd|cut -d':' -f6)"' Which obviously performs an ssh session to server web100 as w100user and then greps ...
3
votes
2answers
438 views

cat-redirect a file, but with variable expansions

I have the file ~/nginx_app containing this conf template: server { root /var/www/html/${domain}/; server_name ${domain} www.${domain}; } I also have this script: #!/bin/bash domain="$1" &...
3
votes
1answer
350 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 ...
3
votes
1answer
5k views

What does the expression ${MYVAR:+-x} mean in bash?

In a bash script I cannot post here I see the following expression: ${MYVAR:+-x} I understand the meaning of expressions like ${MYVAR:+OTHERVAR} and ${MYVAR:-OTHERVAR}, but an expression with both ...
3
votes
1answer
916 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 ...
3
votes
1answer
603 views

Service script calls another script with variables

I have a simple service script tomcat7 in /etc/init.d/ to start and stop my app server (tomcat) under the tomcat user. On RHEL6. #!/bin/bash # description: Tomcat Start Stop # processname: tomcat # ...
3
votes
0answers
508 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, ...
3
votes
1answer
420 views

Does tcsh have an equivalent for “${a:-b}” for default variable assignment like Bash?

I want to do this in one line because I want to use it in my aliases. Can I do this similarly in tcsh ? Using “${a:-b}” for variable assignment in scripts There are some ways of doing it here but ...
2
votes
2answers
2k views

Environment variable not expanded inside the command line argument

I have a file user-pid.out2 which has "usernumber" and "process id" as two columns. based on usernumber I want to find corresponding process id. the first two lines below does not show the output ...
2
votes
4answers
524 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.
2
votes
3answers
169 views

Assigning upper case variables to commands

While trying to decipher some scripts written by former employees of my current company, in many of the scripts, I came across the following statements which assign variables to some commands as shown ...
2
votes
2answers
214 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 ...
2
votes
2answers
2k 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 ...
2
votes
2answers
142 views

Why does zsh allow name to be null in ${name:-word}?

From zsh documentation: ${name-word} ${name:-word} If name is set, or in the second form is non-null, then substitute its value; otherwise substitute word. In the second form name may ...
2
votes
1answer
68 views

Combine brace and variable expansion in one line

I would like to have a logging function that takes filenames as arguments and replicate the stdout to all of these files. This is what I have come up so far: function logger() { exec > >(...
2
votes
3answers
231 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-...
2
votes
1answer
238 views

Can Bash Variable Expansion be performed directly on user input?

In bash, is there any way to read in user input but still allow bash variable expansion? I am trying to request the user enter a path in the middle of a program but since ~ and other variables are ...
2
votes
1answer
1k views

Expand shell variable without globbing

I want to check whether an input string refers to a file name - not a wildcard string like *.txt. This doesn't work: if [ -f "$1" ]; Because $1 gets expanded to *.txt, which gets expanded to, say ...
2
votes
1answer
454 views

shell variable in awk is not being passed to all the lines but just for the very first line of input?

inputfile:is2.txt 10.39.5.41,A1,B1 10.39.5.41,A2,B2 10.39.5.41,A3,B3 10.39.5.41,A4,B4 10.39.5.41,A5,B5 10.39.5.41,A6,B6 script : #!/bin/bash second_column="OOOOOOO" # OOOOOOO will be added to ...
2
votes
2answers
562 views

Picking a random file from each subfolder

Inspired by the accepted answer to this thread: List X random files from a directory, I tried to put together two lines that would pick one file from each subfolder of my current directory, with no ...
2
votes
1answer
2k views

Bash Brace Expansion & Variables [duplicate]

I have a find command: find Directory/{Alpha,Bravo,Charlie} arg1 arg2 I want to replace Alpha,Bravo,Charlie with $find_dir find Directory/{$find_dir} arg1 arg2 however the latter expands to find ...
2
votes
2answers
56 views

What is the right way to quote nested parameter expansions?

When dealing with with nested parameter expansions, which of these is the correct way to quote things? This one: var="${var#"${var%%[![:space:]]*}"}" Or this one: var="${var#${var%%[![:space:]]*}}"...
2
votes
1answer
62 views

How to split an expression by space in bash?

I am getting the valid result I want in zsh but not bash, what am I missing? I am trying to split the string into words. Bash bash-3.2$ echo ${$(echo "Hello World")[@]} bash: ${$(echo "Hello World")[...
2
votes
1answer
27 views

How can I build an env file (with $ characters) listing as-is from echo command?

I am trying to build a file invoked from a script that echoes what is in quotes to a file - only, the $ gets omitted. This is what I was trying to do: echo -e "export ORACLE_BASE=/oracle/APP/grid \n ...
2
votes
3answers
79 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.