Evaluating or expanding a variable to get its value. Depending on the language, a variable may be expanded one or more times.
Questions tagged [variable-expansion]
246 questions
3
votes
1 answer
Expand curly braces and wildcards in a bash string variable (without eval)
Given a string variable (not a string) in bash, how can one expand its curly braces (and glob any file wildcards like asterisks), and then store the resulting expansion in an array?
I know this is possible with eval:
#!/usr/bin/env…

AWitt
- 41
- 3
3
votes
2 answers
Expanding to a flag only if value is set
I have some logic like:
if [[ -n "$SSH_CLIENT" ]]
then
sflag="-s $(echo "$SSH_CLIENT" | awk '{ print $1}')"
else
sflag=''
fi
iptables -A MY_RULE "$sflag" -p tcp -m tcp --dport 9999 -m conntrack -j ACCEPT
In other words, I want to mimic…

Brad Solomon
- 38,521
- 31
- 149
- 235
3
votes
1 answer
Exclamation mark and hash within curly braces in bash
I'm trying to understand a bash script and I'm having trouble with the following line:
result=${!#}
I found part of the solution (! within ${}) here:
If the first character of parameter is an exclamation point (!), it introduces a level of…

Lewistrick
- 2,649
- 6
- 31
- 42
3
votes
2 answers
Powershell variable expansion in single quoted string
I need to build a JSON variable that contains another JSON formatted content which should be formatted as a string. I'm putting the inner JSON into single quote so it wont get parsed along with the outer JSON. The inner JSON contains a variable that…

Royce
- 555
- 1
- 8
- 14
3
votes
1 answer
Why can substring variable expansion reference a variable without a dollar sign?
In bash, to get the first 4 characters of a variable, you can do:
variable='this is a variable'
echo ${variable:0:4}
Instead of hard-coding the length, you can reference a variable like this:
length=4
echo ${variable:0:$length}
However, it seems…

Niko Bellic
- 2,370
- 2
- 29
- 25
3
votes
1 answer
Incorrect %0 in a .BAT file
I have just noticed a very strange behavior of %0 in a .bat file called via %PATH%.
Somewhere in %PATH%, (say, C:\InPath), create file xxx.bat containing:
@echo off
echo this = %~f0
In a different directory, somewhere not in %PATH%, (e.g.…

pepak
- 712
- 4
- 13
3
votes
2 answers
Expand variable before tilde
I'm sure this has been asked before but I can't find anything. We have inscrutable login names on a shared machine and want to use shell variables to substitute the hard-to-remember login names for people's real names.
For example, let's say Omar's…

JohnE
- 29,156
- 8
- 79
- 109
3
votes
1 answer
GNU make: expansion of a deferred construct
On this site containing the documentation describing how make reads a Makefile, there is the following paragraph:
[...]
It’s important to understand this two-phase approach because it has a direct impact on how variable and function expansion…

user42768
- 1,951
- 11
- 22
3
votes
2 answers
Bash variable in curl header as user agent
I was trying to set the user agent dynamic from a bash variable. But it seems that is not that straight forward.
Since the header needs to be enclosed in single quotes if the header value contains spaces, we cannot avoid the single quotes for user…

Jimson James
- 2,937
- 6
- 43
- 78
3
votes
1 answer
Using Groovy for variable expansion in Java properties
I frequently use standard Java property files for configuring my Groovy applications. One feature I have been missing is the ability to use variables as part of the property value so they can be expand dynamically during use. I thought I could…

Ioannis K. Moutsatsos
- 432
- 5
- 13
3
votes
2 answers
String expansion - escaped quoted variable to value
To get started, here's the script I'm running to get the offending string:
# sed finds all sourced file paths from inputted file.
#
# while reads each match output from sed to $SOURCEFILE variable.
# Each should be a file path, or a variable that…

Nick Bull
- 9,518
- 6
- 36
- 58
3
votes
1 answer
mv not Recognizing Wildcards/Variable Expansion
I'm attempting to move all the files in a series of directories into a subdirectory of their respective folder. There are many folders that need to have this happen to, so I've put it in a loop. For the sake of the example, I've reduced the…

mkingsbu
- 354
- 3
- 20
3
votes
2 answers
Expand string Variable stored via Single Quote in Powershell
I have a scenario where I need to construct a powershell path as $RemotePath = '$($env:USERPROFILE)\Desktop\Shell.lnk'. This variable gets passed to a remote machine where it needs to be executed. The remote machine receives this as a string…

Rohit Mitra
- 162
- 4
- 13
3
votes
2 answers
How do I echo $command without breaking the layout
I'm trying to do the following in a bash script:
com=`ssh host "ls -lh"`
echo $com
It works, but the echo will break the output (instead of getting all lines in a column, I get them all in a row).
If I do: ssh host ls -lh in the CLI it will give…

w00t
- 616
- 3
- 9
- 16
3
votes
4 answers
Remove Trailing \ in subst Command
I have added a command to my Context Menu via the Registry in HKCR\Drive\shell\MapLocalDriveHere\command such that when I right click a drive. I'd like it to give me the name of the drive that I have right-clicked as "C:" not "C:\", as this causes…

Ehryk
- 1,930
- 2
- 27
- 47