The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Questions tagged [variable-substitution]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
2
votes
1answer
1k views

How achieve variable indirection (refer to a variable whose name is stored in another variable) in tcsh

I'm trying to do the following: set a = kitten set temp_kitten = purr echo ${temp_$a} I want the echo command to return "purr". The overall idea is that I have a bunch of variables in an array and ...
2
votes
2answers
75 views

Using variable values defined in one file, in files in the same directory

In this GitHub repository I have a directory named nwsm. This directory contains the file nwsm.sh that contains a master script (a script that runs other scripts). The directory also contains a few ...
2
votes
1answer
1k views

Passing variables to range patterns in awk

I'm trying to use range patterns in awk: grep -a volume somefile | awk '/^Apr 25 23:44:04*/,/^Apr 26 12:44:01*/ {print}' This works fine, but when I try to execute it by putting the date and time in ...
2
votes
2answers
559 views

assign output of a c program to a shell variable: token error

I have a shell script to run several times a .c program ('switch') which admit 3 input paramenters. I want to run the program 4 times passing values {4,16,32,64}, and for parameters {0.1,0.2,.3,.4,.5,...
2
votes
1answer
184 views

Parameter substitution and error messages: suppressing line numbers etc

Here is my example file: !/bin/bash # argument-one # Is first argument missing? # First method [[ "$1" == "" ]] && echo "Usage: $(basename $0) filename"; exit 1 # Second method # filename=$...
2
votes
1answer
4k views

What does the expression ${MYVAR:+-x} mean in bash?

In a bash script I cannot post here I see the following expression: ${MYVAR:+-x} I understand the meaning of expressions like ${MYVAR:+OTHERVAR} and ${MYVAR:-OTHERVAR}, but an expression with both ...
2
votes
1answer
4k views

bash: export: `--wait': not a valid identifier

When I run git-sh from my terminal it works fine. But I get the following warning. bash: export: `--wait': not a valid identifier What is this and how do I get rid of it?
2
votes
2answers
348 views

Wildcard not being interpreted in if statement

I am currently writing the below script. The code looks at a certain directory for a filename inputted by the user. The script first checks to see if the input file is a gzip, if so, it runs the ...
2
votes
1answer
145 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
2answers
860 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 ...
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 ...
2
votes
2answers
82 views

variable substitution in CLI and for loops

I am pretty sure this has been asked already in some form, I just cannot come up with a good find to search it. I want to have a script that do things N times and to which I can pass as a variable ...
2
votes
2answers
127 views

Read command in shell script - variable input substitution

I am new to Linux, pardon any wrong terminology used here. I am using Ubuntu 18.04. I am reading text in a shell script using read command. I have a variable in bash window that I want to substitute ...
2
votes
0answers
78 views

What are the contexts where Bash doesn't perform word splitting and globbing?

Bash doesn't perform word splitting in globbing in these cases: LHS or RHS of an assignment, except for indexed arrays var=$value # simple variable declare -A hash key=...
2
votes
0answers
448 views

Replacing *any string* dynamically with POSIX shell [duplicate]

I'm developing a POSIX compliant script in which I want to do a dynamic replace of strings, something like: sed "s/${pattern}/${replace}/g" Problem: if the pattern includes the delimiting character, ...
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 ...
2
votes
1answer
144 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)...
2
votes
2answers
612 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,...
1
vote
3answers
290 views

Dynamic variables in shell

I have a following script that will take input (source path) from user and it will attach the volume inside docker container echo -n "Enter the source path: " read path docker run -v $path:/opt/$...
1
vote
3answers
455 views

Trivial rm -rf command destroys my operating system in a testing machine

I use Bash 4.3.48(1) and I ran the following command pattern on a testing VPS machine: rm -rf ${drt}/${pma}* This command deleted the entire operating system (Ubuntu). That was evident by executing ...
1
vote
3answers
95 views

How to dynamically create Bash code with a loop and execute it as it is created

I have a list of lines in a Bash script as follows if [ ! -z "$clone01" ]; then git clone "$clone01"; fi if [ ! -z "$clone02" ]; then git clone "$clone02"; fi if [ ! -z "$clone03" ]; then git clone "$...
1
vote
1answer
918 views

Glob character within variable expands in bash but not zsh

I'm seeing an issue with zsh where a glob character within a variable is not expanding as I would expect. The following example does a better job of explaining it. $ echo $0 -bash $ echo $HOME/...
1
vote
2answers
208 views

sed /PATTERN/SUB/ $VAR?

I am renaming some files. This works: ls | while IFS= read -r line; do name=$(echo $line | sed -e 's/\(.*\)/\1.jpg/') && mv $line $name; done Which is okay but I'd like to make it more ...
1
vote
1answer
995 views

Replacing shell variable names in another variable

I've been wrestling with bash variable substitution for a while now and can't figure this out... I have a variable with a command template: CMD_TMPL='sudo -u ${USER_NAME} ${USER_HOME}/script.sh' ...
1
vote
1answer
127 views

why curl can't expand variable inside the loop, but echo can?

I need to include variable inside curl for fname in "assets/*.drx"; do echo $fname curl -F file=@"$fname" "http://someurl.com/" echo $fnamename done Result: assets/...
1
vote
2answers
63 views

properly using the $ atom in a parameter substitution

I'm trying to perform a parameter substitution that strips everything from the first - char to the end of a string like so: v0.1-bla-hblah-232 -> v0.1 So I'm using the following script: #!/usr/...
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
136 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 ...
1
vote
1answer
170 views

Passing paths and filenames to a program from bash script

I use a utility called mp3wrap which combines multiple mp3s into one. Say I have a directory "album 1" containing three files "track 1.mp3", "track 2.mp3" and "track 3.mp3". I will run the following: ...
1
vote
2answers
202 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?
1
vote
1answer
144 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 ...
1
vote
2answers
398 views

Substrings in shell script

Am trying to get a substring from a string but am getting the error: ${curr_rec:3:4}: bad substitution #!/bin/ksh get_file_totals() { if [ -e "$file_name" ] then IFS='' ...
1
vote
3answers
150 views

Variable scope in multiple pipes

I was trying to do something following, find . -name "*.dat" | get the basename of file | move filename returned by first command to basename returned by second command To give a concrete example, I ...
1
vote
1answer
2k views

Shell variables not working?

I'm trying to store the date in a variable ($DATE) so I can use it later, but it doesn't seem to work. See anything wrong? DATE=$(date +"%Y-%m-%d %R") sudo mysqldump -u root -pnotgivingyoumypassword -...
1
vote
2answers
3k views

One line env variable set behavior [duplicate]

If I do this: SUMAN_DEBUG=foo echo $SUMAN_DEBUG I get no output but if I do: SUMAN_DEBUG=foo && echo $SUMAN_DEBUG then I get "foo" why is that?
1
vote
1answer
98 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
1answer
427 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, ...
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="...
1
vote
1answer
5k 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 ...
1
vote
1answer
1k views

special symbols, . * # inside curly brackets [duplicate]

bash=${BASH_VERSION%.*}; bmajor=${bash%.*}; bminor=${bash#*.} echo "BASH VERSION --- $BASH_VERSION" echo "bmajor ----- $bmajor" echo "bminor ----- $bminor" prints, BASH VERSION --- 4.2.46(1)-release ...
1
vote
1answer
216 views

cp runs from command line but file not found from script [duplicate]

I have a script to reorganize my directory structure that contains: files=./*"$pattern"I{"$ifirst".."$ilast"}.ext cp ${files} ../"$pattern"/"$i"/ When I run the script, I get an error: ./*S6I{001.....
1
vote
1answer
35 views

Zsh: check existing of file before expanding

In Zsh parameter expansion, I have: "${test_var:-"${HOME}/test"}" but here I want to check if ${HOME}/test is exist or not, if not exist then test_var expand to " " (one space string). Is there an ...
1
vote
1answer
918 views

Makefile: Copy using make variables --> error; Not so without variables!

In the following makefile InputLocation:=./Test OutputLocation:=$(InputLocation)/Output Input:=$(wildcard $(InputLocation)/*.md) Output:=$(patsubst $(InputLocation)/%, $(OutputLocation)/%, $(Input:...
1
vote
1answer
220 views

Basic usage of /etc/cron/ (d): correct pattern for /etc/cron (daily/weekly/monthly)

I use Ubuntu 16.04 with Bash and I've created this extensionless, shebangless file /etc/cron.daily/cron_daily: for dir in "$drt"/*/; do if pushd "$dir"; then wp plugin update --all --allow-root wp ...
1
vote
1answer
480 views

Run cd with path variable in bash script not working [duplicate]

When i run the script: #!/bin/bash DRUPAL_ROOT=$(drush status root --format=list) if [ -z $DRUPAL_ROOT ] then echo -e "Not exists Drupal core" else echo $DRUPAL_ROOT cd $DRUPAL_ROOT fi Output: ...
1
vote
1answer
259 views

How to retrieve a part of a path name and assign it to a variable?

I want to assign a part of my current working directory path name to a variable and use it in a script inside the directory itself. For eg: If my pwd is : /home/desktop/project/ABC/abc/abc_123, is ...
1
vote
1answer
260 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
843 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 ...
1
vote
2answers
601 views

bash + how to define array variable with instance number

Is it possible to define variable that is called for example machine1 as machine$counter ( while counter=1 ) ? For example, I created the /tmp/config.txt file and set the machine1 as array: $ more /...
1
vote
1answer
424 views

Unknown syntax in /etc/rc.d/init.d/functions

Looking at the file /etc/rc.d/init.d/functions, I can see the following is declaring a number of variables as local variables. However, why are the variables killlevel and pid_file have an equal sign ...