Questions tagged [variable-expansion]

Evaluating or expanding a variable to get its value. Depending on the language, a variable may be expanded one or more times.

246 questions
956
votes
7 answers

When do we need curly braces around shell variables?

In shell scripts, when do we use {} when expanding variables? For example, I have seen the following: var=10 # Declare variable echo "${var}" # One use of the variable echo "$var" # Another use of the variable Is there a significant…
New User
  • 9,759
  • 4
  • 15
  • 8
182
votes
8 answers

How does the Windows Command Interpreter (CMD.EXE) parse scripts?

I ran into ss64.com which provides good help regarding how to write batch scripts that the Windows Command Interpreter will run. However, I have been unable to find a good explanation of the grammar of batch scripts, how things expand or do not…
Benoit
  • 76,634
  • 23
  • 210
  • 236
134
votes
34 answers

What is the most elegant way to remove a path from the $PATH variable in Bash?

Or more generally, how do I remove an item from a colon-separated list in a Bash environment variable? I thought I had seen a simple way to do this years ago, using the more advanced forms of Bash variable expansion, but if so I've lost track of it.…
Ben Blank
  • 54,908
  • 28
  • 127
  • 156
109
votes
7 answers

How to obtain the first letter in a Bash variable?

I have a Bash variable, $word, which is sometimes a word or sentence, e.g.: word="tiger" Or: word="This is a sentence." How can I make a new Bash variable which is equal to only the first letter found in the variable? E.g., the above would…
Village
  • 22,513
  • 46
  • 122
  • 163
75
votes
4 answers

How do SETLOCAL and ENABLEDELAYEDEXPANSION work?

I notice in most scripts, the two are usually in the same line as so: SETLOCAL ENABLEDELAYEDEXPANSION Are the two in fact separate commands and can be written on separate lines? Will setting ENABLEDELAYEDEXPANSION have an adverse effect on a script…
Anthony Miller
  • 15,101
  • 28
  • 69
  • 98
30
votes
6 answers

Performance of variable expansion vs. sprintf in PHP

Regarding performance, is there any difference between doing: $message = "The request $request has $n errors"; and $message = sprintf('The request %s has %d errors', $request, $n); in PHP? I would say that calling a function involves more stuff,…
elitalon
  • 9,191
  • 10
  • 50
  • 86
30
votes
7 answers

Brace expansion with variable?

#!/bin/sh for i in {1..5} do echo "Welcome" done Would work, displays Welcome 5 times. #!/bin/sh howmany=`grep -c $1 /root/file` for i in {1..$howmany} do echo "Welcome" done Doesn't work! howmany would equal 5 as that is what the output of…
user2891460
26
votes
2 answers

Expand variable inside single quotes

How can I expand $pw inside single quotes? $pw = "$PsHome\powershell.exe" cmd.exe /c 'schtasks /create /tn cleanup /tr "$pw -WindowStyle hidden -ExecutionPolicy Bypass -nologo -noprofile %TEMP%\exec.ps1" /sc minute /mo 1'
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
19
votes
4 answers

get a default value when variable is unset in make

(edit: question more accurate based on @Michael feedback) In bash, I often use parameter expansion: the following commands print "default value" when $VARNAME is unset, otherwise it prints the VARNAME content. echo ${VARNAME:-default value} #if…
oHo
  • 51,447
  • 27
  • 165
  • 200
16
votes
3 answers

bash send string argument as multiple arguments

So I have a bash script that is generating a string of commands that I want to be run. The output looks something like $ program "command1 command2 ... commandN" But the program is interpreting (as it should) "command1 command2 ... commandN" as one…
trev9065
  • 3,371
  • 4
  • 30
  • 45
13
votes
2 answers

How to expand variables in vim commands?

I'm trying to get a variable expanded in a command call. Here's what I have in my .vimrc: command! -nargs=1 -complete=dir TlAddPm call s:TlAddPm() function! s:TlAddPm(dir) let flist = system("find " . shellescape(a:dir) . " -type f -name…
derobert
  • 49,731
  • 15
  • 94
  • 124
11
votes
5 answers

How do I delay expansion of variables in PowerShell strings?

Whatever you want to call it, I'm trying to figure out a way to take the contents of an existing string and evaluate them as a double-quoted string. For example, if I create the following strings: $string = 'The $animal says "meow"' $animal =…
Adi Inbar
  • 12,097
  • 13
  • 56
  • 69
10
votes
1 answer

Passing a command with arguments as a string to docker run

The issue I'm facing is how to pass a command with arguments to docker run. The problem is that docker run does not take command plus arguments as a single string. They need to be provided as individual first-class arguments to docker run, such…
davidA
  • 12,528
  • 9
  • 64
  • 96
9
votes
1 answer

What's wrong with the following GNU make shell variable expansion?

On this line: GCCVER:=$(shell a=`mktemp` && echo $'#include \nmain() {printf("%u.%u\\n", __GNUC__, __GNUC_MINOR__);}' | gcc -o "$a" -xc -; "$a"; rm "$a") I get: *** unterminated call to function `shell': missing `)'. Stop. What's wrong…
Matt Joiner
  • 112,946
  • 110
  • 377
  • 526
9
votes
1 answer

What does "${!var}" mean in shell script?

I have a code block with below condition, not sure what exactly it does. $var = "${args}_Some_Text" if [ "${!var}" == '' ];then echo "$var is not defined !!!" fi
tempuser
  • 1,517
  • 3
  • 11
  • 15
1
2 3
16 17