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
9
votes
3 answers
Expanding a block of numbers in Python
Before I asked, I did some googling, and was unable to find an answer.
The scenario I have is this:
A list of numbers are passed to the script, either \n-delimited via a file, or comma-delimited via a command line arg. The numbers can be singular,…

Shadow Master
- 121
- 4
9
votes
1 answer
What is the 'reword' function in Rebol and how do I use it?
I saw someone mention the reword function today, but documentation for it is very brief. It looks like shell script environment variable substitution, or maybe regex substitution, but different. How do I use this function and what kind of gotchas am…

Adrian
- 741
- 4
- 14
8
votes
1 answer
Word splitting in Bash with IFS set to a non-whitespace character
I'm going through a Bash tutorial, and specifically the subject of word splitting.
This script, called "args", helps demonstrate word splitting examples:
#!/usr/bin/env bash
printf "%d args:" $#
printf " <%s>" "$@"
echo
An example:
$ ./args hello…

Nadim Hussami
- 349
- 2
- 14
8
votes
1 answer
Bash : Adding extra single quotes to strings with spaces
When I try to passing arguments as variables to any commands in bash I can see extra quotes added by bash if the variable value has spaces.
I am creating a file "some file.txt" and adding it to a variable $file.
I am using $file and storing it in…

anoop.babu
- 405
- 2
- 6
- 13
8
votes
4 answers
omit passing an empty quoted argument
I have some variables in a bash script that may contain a file name or be unset. Their content should be passed as an additional argument to a program. But this leaves an empty argument when the variable is unset.
$ afile=/dev/null
$…

XZS
- 2,374
- 2
- 19
- 38
6
votes
3 answers
How to do a partial expand in Snakemake?
I'm trying to first generate 4 files, for the LETTERS x NUMS combinations, then summarize over the NUMS to obtain one file per element in LETTERS:
LETTERS = ["A", "B"]
NUMS = ["1", "2"]
rule all:
input:
expand("combined_{letter}.txt",…

bli
- 7,549
- 7
- 48
- 94
6
votes
3 answers
How to create for-loops with jq in bash
I'm trying to split a json file into various json files. The input (r1.json) looks like :
{
"results" : [
{
content 1
}
,
{
content 2
}
,
{
content n
}
]
}
I'd like the output to be n…

crocefisso
- 793
- 2
- 14
- 29
6
votes
2 answers
Bash arbitrary glob pattern (with spaces) in for loop
Is there any way to reliably use an arbitrary globbing pattern that's stored in a variable? I'm having difficulty if the pattern contains both spaces and metacharacters. Here's what I mean. If I have a pattern stored in a variable without spaces,…

Josh Hults
- 63
- 3
6
votes
1 answer
ln complains about no such file or directory
I'm new in shell programming on macosx and have a little problem. I've written the following shell script:
#!/bin/sh
function createlink {
source_file=$1
target_file="~/$source_file"
if [[ -f $target_file ]]; then
rm $target_file
fi
ln…

Jan Baer
- 1,105
- 1
- 12
- 15
5
votes
1 answer
What exactly is “%~zI” expanded to for directories in FOR loops?
From FOR /?:
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:
%~I - expands %I removing any surrounding quotes (")
%~fI - expands %I to a fully qualified…

Константин Ван
- 12,550
- 7
- 61
- 73
5
votes
2 answers
Using expanding strings as Powershell function parameters
I'm trying to write a function that will print a user-supplied greeting addressed to a user-supplied name. I want to use expanding strings the way I can in this code block:
$Name = "World"
$Greeting = "Hello, $Name!"
$Greeting
Which…

vazbyte
- 53
- 5
5
votes
1 answer
Get data from elements within multiple XML files for output to another, single XML file using Powershell
I'll begin by confessing that I'm a Powershell (and coding) noob. I've stumbled my way through a few scripts, but I make no claims to anything even approaching competence. I'm hopeful that some more experienced folks can set me on the right…

M Jenks
- 85
- 4
5
votes
2 answers
Bash variable expansion with a '/'
Inside of a while read line loop, I see this variable expansion ${line/device name:}. I've tried running the script with my own input file and it just prints out the line.
Can you tell me what that expansion is doing?

Jimbo
- 51
- 1
5
votes
1 answer
How to expand the elements of an array in zsh?
Say I have an array in zsh
a=(1 2 3)
I want to append .txt to each element
echo ${a}.txt # this doesn't work
So the output is
1.txt 2.txt 3.txt
UPDATE:
I guess I can do this, but I think there's a more idiomatic way:
for i in $a; do
echo…

nachocab
- 13,328
- 21
- 91
- 149
5
votes
3 answers
Grep error due to expanding variables with spaces
I have a file called "physics 1b.sh". In bash, if I try
x="physics 1b"
grep "string" "$x".sh
grep complains:
grep: physics 1b: No such file or directory.
However, when I do
grep "string" physics\ 1b.sh
It works fine. So I guess the problem is…

nightfire
- 815
- 2
- 12
- 22