Questions tagged [variable-substitution]

Use where the name of a variable is textually substituted with the value of the variable

Variable substitution is the textual process of inserting the value of a variable (possibly in some modified way) in the context where the variable's name (typically with some syntax indicator that it is a variable's name) stands.

312 questions
84
votes
1 answer

Variable substitution with an exclamation mark in bash

I have the following lines in my .cfg bash script file DDF_SOURCE="siebel_DATA_DATE_FORMAT" DATA_DATE_FORMAT=${!DDF_SOURCE} how is ${!DDF_SOURCE} evaluated? It would be !siebel_DATA_DATE_FORMAT, which doesn't make sense to me.
van
  • 1,089
  • 1
  • 9
  • 9
58
votes
6 answers

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 other programs. My goal is to enable a script to…
Walf
  • 1,254
  • 1
  • 14
  • 19
45
votes
4 answers

Use a variable reference "inside" another variable

I'm sure it is relatively simple, I just don't know how to do it. #!/usr/bin/ksh set `iostat` myvar=6 I want to something like echo ${$myvar} which i want interpreted as ${$myvar} -> ${6} -> value
Brandon Kreisel
  • 1,058
  • 2
  • 11
  • 14
39
votes
7 answers

Double and triple substitution in bash and zsh

Follow-up to the background part in this question. In bash I can use ${!FOO} for double substitution, in zsh ${(P)FOO}. In both, the old-school (hack-y) eval \$$FOO works. So, the smartest and most logical thing for me would be ${${FOO}},…
Profpatsch
  • 1,749
  • 4
  • 20
  • 23
39
votes
2 answers

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
Talespin_Kit
  • 557
  • 1
  • 5
  • 10
36
votes
3 answers

What does ${PATH:+:${PATH}} mean?

I recently noticed the following in my cygwin profile, more precisely: /usr/local/bin:/usr/bin${PATH:+:${PATH}} What does it mean? Why is not just $PATH? Is this an 'if $PATH exists then add :$PATH'? My purpose is to swap the order and put the…
tofutim
  • 463
  • 1
  • 4
  • 5
33
votes
3 answers

Why is my variable local in one 'while read' loop, but not in another seemingly similar loop?

Why do I get different values for $x from the snippets below? #!/bin/bash x=1 echo fred > junk ; while read var ; do x=55 ; done < junk echo x=$x # x=55 .. I'd expect this result x=1 cat junk | while read var ; do x=55 ; done echo x=$x # …
Peter.O
  • 32,426
  • 28
  • 115
  • 163
31
votes
2 answers

How to avoid space after bash variable in string?

I have a variable for color. I use it to set color to strings, by evaluating it inside the string. However, I need to include space after the name(so that the name doesn't contain part of the text). This sometimes looks bad. How can I avoid…
MatthewRock
  • 6,826
  • 6
  • 31
  • 54
27
votes
4 answers

What is the equivalent of bash indirect referencing ${!FOO} in zsh?

${!FOO} performs a double substitution in bash, meaning it takes the (string) value of FOO and uses it as a variable name. zsh doesn’t support this feature. Is there a way to make this work the same in bash and zsh? Background: I’ve got a list of…
Profpatsch
  • 1,749
  • 4
  • 20
  • 23
25
votes
6 answers

Is there any way to print value inside variable inside single quote?

Consider I've set variable site and needs to be printed by echo or printf, but If I use single quote to write something and want to use variable then how? Example: $ site=unix.stackexchange.com $ echo "visit:$site" visit:unix.stackexchange.com But…
Pandya
  • 23,898
  • 29
  • 92
  • 144
24
votes
6 answers

Bash variable substitution in a JSON string

I'm trying to create a JSON in BASH where one of the fields is based on the result of an earlier command BIN=$(cat next_entry) OUTDIR="/tmp/cpupower/${BIN}" echo $OUTDIR JSON="'"'{"hostname": "localhost", "outdir": "${OUTDIR}", "port": 20400,…
Guru Prasad
  • 363
  • 1
  • 3
  • 6
21
votes
3 answers

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. At the moment I use descr=$(grep -oP '\->\s*\S+' <<< "$descr"…
Martin
  • 7,284
  • 40
  • 125
  • 208
21
votes
2 answers

Append to PATH-like variable without creating leading colon if unset

I need to append a directory to PKG_CONFIG_PATH. Normally, I would use the standard export PKG_CONFIG_PATH=${PKG_CONFIG_PATH}:$(pyenv prefix)/lib/pkgconfig but PKG_CONFIG_PATH has not been previously set on my system. Therefore, the variable begins…
scottbb
  • 548
  • 6
  • 15
17
votes
2 answers

Passing a variable to sed

I cannot not use a shell variable in sed in the $NUMBER form. I have this line in my shell script: cat shared.txt sed 's/whatever1/$2 ... whatever2/' > shared2.txt The result in shared2.txt looks like: ...$2.... What did I do wrong?
user2013619
  • 355
  • 1
  • 2
  • 6
15
votes
3 answers

bash ${VAR//search/replace} and weird regex behaviour

I am trying to do some searching and replacing on a variable using the ${VAR//search/replace} parameter expansion. I have a pretty long and evil PS1, that I want to work out the size of after expansion. To do so I have to remove a bunch of escape…
Drav Sloan
  • 14,145
  • 4
  • 45
  • 43
1
2 3
20 21