Questions tagged [variable-substitution]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
3
votes
1answer
681 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 ...
7
votes
2answers
606 views

Any way to show each step during the command processing?

set -x just show a final result of the command. Any way to view each step more clear, like pics below.
1
vote
3answers
64 views

Use nested var for substitute var

I've a big question today ! I've made a little exemple of my situation. FOO="foo"; fooBAR="final"; var=${${FOO}BAR} echo $var I want to print "final". I tried all sort of things like ${${!FOO@}BAR} ...
1
vote
1answer
90 views

Declare as local var will break a function and log out “1: number expected”

function projectopen { local di_files=(*.xcworkspace */*.xcworkspace *.xcodeproj */*.xcodeproj) # open first exsit file ls -d -f -1 $di_files 2>/dev/null \ | head -1 \ | xargs ...
1
vote
3answers
73 views

Use a list file to edit a master file through awk

everyone. I have two files: ports.lst and master.tbl ports.lst looks like this: hawaii-P1 hawaii-P2 hawaii-P3 losangeles-P1 losangeles-P3 master.tbl looks like this: #Site 1 Honolulu servername ...
1
vote
2answers
197 views

Why a value of a variable doesn't change inside backticks?

Here's my test code: a=1 echo $a echo `let ++a` echo $a The output that I see is 1, 1. Why doesn't the third line modify the value of a?
0
votes
2answers
3k views

How do I call a script with a wildcard pattern and two more arguments?

I have this code to rename a bunch of files: #!/bin/bash for file in $1 do mv -i "${file}" "${file/pattern/replacement}" done How do I replace "pattern" and "replacement" with $2 and ...
1
vote
0answers
41 views

To divide a a loop variable per se and save in another one to be used later

I am facing a problem with bash script: I have a set of variables 'i' that ranges from 3.31 up to 3.35. My script prints out, as a function of the variable 'i' some input file for geometrical ...
2
votes
1answer
140 views

Wordsplitting occurring in quoted variable

I'm trying to create a dialog menu based on the results of the lsblk command. My goal is to read the first word as the menu item and the remaining words as the menu item description. For example: ...
2
votes
1answer
221 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 ...
6
votes
2answers
22k views

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": "...
2
votes
0answers
514 views

Running R script and global variable overriding with <<- from cron

I have a R script that works just fine when I run it from RStudio. However, once I schedule it through cron under user RStudio only with home directory (/home/rstudio), it halts at the very step I ...
0
votes
2answers
1k views

How do I assign last month's date (year and month only - 2016-07) as a variable? [duplicate]

I am having trouble with using date as a variable in a bash script. I will admit that I have zero experience writing anything – in bash or otherwise – but I’ve been tasked with figuring this out. I ...
3
votes
2answers
1k 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 ...
2
votes
2answers
5k views

How to assign value to input variable in shell

I am asking user for input and taking input in the variables such as $1, $2 etc. I am checking the variable for null value and wants to replace the value if null. if [ "$2" == "" ]; then 2=value fi ...
9
votes
2answers
7k views

Grep to find the correct line, sed to change the contents, then putting it back into the original file?

I'm trying to change a single word on a specific line in a file, but I'm having some trouble connecting all together. Basically, on one line in my file there is a keyword 'firmware_revision', and on ...
12
votes
1answer
664 views

See the expansion of Bash variables for debugging?

Is there any tools/tricks available for expanding variables to make bash code easier to read? For example it would convert the following: DIR=/var/tmp FILE=${DIR}/file SCRIPT_ROOT=/opt/root TOOL=${...
3
votes
4answers
300 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....
1
vote
1answer
136 views

Possible to reuse first argument of BASH line in the same line?

I do this in BASH echo test "$1" ..expecting to get.. test test ..but I get.. test Is this something possible to do? It would make my life easier since having a list files I could do something ...
2
votes
2answers
1k views

Defer variable expansion to subshell

My service uses environment variables extensively for configuration (mostly because it's really easy to inject those into a Docker container). I also have some maintenance scripts that I can run ...
0
votes
0answers
96 views

How to resolve a variable's value inside an array in bash

I have a few files whose names I have assigned into variables. (There may be n number of files). Those variables are passed into an array. Now I want to print the names of the original files using ...
0
votes
2answers
274 views

What's the proper way to use a variable with spaces in part of a shell script command? [duplicate]

How can I include spaces as part of a variable used in an svn command for RHEL bash scripting? Or if there's something else wrong with the following, please advise. The SVN URL variable has no spaces,...
4
votes
2answers
1k 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 | ...
6
votes
1answer
1k views

Variable with colon dash ${VAR:-}

How does "${2:-}" work in an 'if-then-else example' below? Somehow I cannot help but think that being ${2:-} it means the second argument, but I am curious what the colon(:) and dash(-) after the ...
0
votes
1answer
139 views

Bash add text on line script [closed]

Trying to get my script to work but it won't work. Here is my script #!/bin/bash a="* * * * * /home/server/scripts/check.sh" b="0 06,18 * * * /home/server/scripts/checks2.sh" sudo sed -i '28s/^/'$b'\...
1
vote
1answer
406 views

Creating a complex variable content by hand

I am creating a shell script dynamically and sending it to a remote server for execution. All is working except when I try to inject a variable with content coming from a file with multiple lines, ...
0
votes
1answer
105 views

Why does parameter expansion happen before brace expansion in this example?

From https://unix.stackexchange.com/a/7739/674, why does parameter expansion happen before brace expansion in the following example? eval rm foo.{$ext0..$extN}
17
votes
2answers
10k views

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). ...
0
votes
1answer
745 views

Shell script that outputs chmod command for all files

On my EC2 Linux server, I blindly ran chmod 777 at directory that it turns out I should not have. There are hundreds of files in there, and I'm not sure which ones need to go back to their original ...
0
votes
2answers
652 views

/usr/bin/find invocation doesn't work when used with command substitution [duplicate]

In a script, I have: CMD='/usr/bin/find /etc -type f -exec /usr/bin/md5sum {} \; ' MD5=$("${CMD}") But the script gives the error: -bash: /usr/bin/find /etc -type f -exec /usr/bin/md5sum {} \; : ...
3
votes
1answer
364 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 ...
1
vote
1answer
254 views

Bash: How to get a value from an array to create command from it

Is it possible to use bash to fetch a string from an array provided in the following syntax within a config file? | | Version | PackageManager | Webserver | Database | |--------|---------|-----...
1
vote
1answer
672 views

Error with find when using -newerct values stored in variables

I'm trying to create a bash function that will use a simple file such as sample.txt start=22 Mar 2016 10:00 end=22 Mar 2016 12:09 ...and find files that I have installed to /usr/local within the ...
2
votes
2answers
810 views

Multiple lines of input for multiple variables

I would like to enter multiple lines of values for 3 different variables. For example, lets say I have $a, $b, and $c in a line like: ADD $a in $b to get $C My input will be in rows for all 3 ...
0
votes
1answer
170 views

How can I mix variables and wildcards in fish?

I have a value stored in a variable that I want to use as part of a wildcard, like this: set extension (somecommand) cp *$extension ~/filezone But the * prevents the variable from being dereferenced....
1
vote
2answers
3k views

Bash: Echo a variable whose name is the value of another variable [duplicate]

Suppose I have the following: foo1=abc i=1 a="FOO${i}" echo ${${a}} echo ${`echo $a`} # I also tried that I am getting the error bash: ${${a}}: bad substitution.
1
vote
1answer
128 views

Any way to create Here Documents that substitute some variables but not others in bash?

I was wondering if there any way to create a here document in bash to output to a file within a script, problem is that in this config file I am trying to output some variables that start with $ I ...
5
votes
2answers
761 views

How can I add datetime to an existing file name in Linux?

I want to add date and time to the file name, for example: 08032016out.log.zip This is what I try to do: _now=$(date +"%m_%d_%Y") _file="$_nowout.log" touch $_file.txt # creating new file with the ...
23
votes
2answers
5k views

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'?...
1
vote
1answer
820 views

bash adds apostrophes to expanded variable

I am reading in a file inside my bash script where each line consists of multiple columns like this: ANSIBLE 'first run' '--diff --extra-vars "client=yes fast=no"' As you can see, the line consists ...
2
votes
3answers
157 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
1answer
139 views

Bash: Default variable with command substitution

Is there any way to accomplish the following in one stroke using Bash? my_var=${$(grep -E '^setting_1' /settings.conf):-default_setting1} As opposed to: my_var=$(grep -E '^setting_1' /settings.conf)...
7
votes
1answer
1k views

Append (alter) each array element via parameter expansion (i.e. without printf)?

Let the script below exemplify my quandary.. #!/bin/zsh STUFF=( moose-hoof ovary clydsedale ) echo ${MINE=$(printf "MY-%s " $STUFF)} echo ${MINE_EXP=${STUFF/^/MY-}} MY-moose-hoof MY-ovary MY-...
6
votes
2answers
815 views

Why do here-documents attempt shell substitution even on a commented line?

I wanted to see how some ASCII art looked in terminal so: $ cat <<EOF ...
1
vote
4answers
43 views

Testing for any of a family of related values in a Bash variable [closed]

Apologies if this has been answered previously, but I couldn't find much online. Here's what I'm trying to accomplish: if [[ "${hostname}" = (foo | bar)servername(01 | 02 | 03) ]]; then var="...
2
votes
2answers
46 views

rename of file extension doesnt work [closed]

I'm using code like the following to create a logfile using the shell's string manipulation tools: I have a file name and, in the case of an error, I need to create a log file with the err. Suppose ...
0
votes
1answer
342 views

Imagemagick having trouble escaping shell variables

I have an imagemagick command that is having trouble with the $quote$arcangle and $halfdimension$quote concatenated shell variables I'm passing as strings. Here's the transcript: $> echo convert ...
1
vote
1answer
4k views

Passing a variable argument to egrep in a bash script

I have a script, myScript, which is trying to egrep the script argument in a file. Somehow variable expansion isn't working properly with the egrep command. I believe I've isolated the problem ...
0
votes
2answers
129 views

Find the position of repeated character “&”

How can determine the position of “&” in the column number 5 to substring the value 2 or substring value 3 by awk functions column1|column2|column3|column4|variable1:value1 & variable2:...
2
votes
2answers
571 views

while-loop doesn't seem to remember variable update - no piping involved

I am running LMDE 2 ( 'Betsy') with the standard Mate desktop and I have the following script: #!/bin/bash # This script starts a specified terminal-binary in "Always on Top"-mode # The assumption is,...