Skip to main content

Questions tagged [variable-substitution]

Use where the name of a variable is textually substituted with the value of the variable

Filter by
Sorted by
Tagged with
0 votes
0 answers
17 views

bash variable problem [duplicate]

Apologies if this is nooby question but I am new to bash scripting and I couldn't find anything similar. I am trying to use ffmpeg to re-encode a load of files and that requires I pull out some ...
Andy Ashwood's user avatar
7 votes
1 answer
586 views

printf - store formatted string output in a variable

Instead of the following printf '%s\t%s\t%s\t%s\n' 'index' 'podcast' 'website' 'YouTube' I want to store the printf output in a Results variable, how can I do this?
Porcupine's user avatar
  • 2,056
-1 votes
1 answer
57 views

Output of loop variable shows different value than expected

Imagine I have two folders in my current working directory: back/ and front/. I am trying to enter inside each of them and do some stuff. From the simple script below: for dir in */ ; do echo &...
Rigel F. do C.'s user avatar
0 votes
2 answers
87 views

how to replace a string with a variable?

I am trying to write a script containing a loop that enters a series of directories containing the same file, to replace a text string in a file with a variable that matches the directory's name. The ...
Ella Brudner's user avatar
5 votes
2 answers
433 views

Issue expanding variable with multiple wildcards in bash shell script with mv / rename

I wrote a bash shell script for Linux to move files in a static folder according to parameters specified in a data file. I've reduced that script to a minimal reproducible example to demonstrate an ...
End Antisemitic Hate's user avatar
2 votes
3 answers
865 views

Execute copy commands from file

I have a DE_CopyOldToNew.txt file with a whole bunch of copy commands for copying old file names to new file names. The file contains rows like : cp /migrationfiles/Company\ Name\ GmbH/2014.138_Old\ ...
Rico Strydom's user avatar
0 votes
1 answer
58 views

Why does this script variable keep showing as "null"?

I have the following script: def String Service_version = sh "cd ./projects/onehome && pwd && cat package.json | grep version" echo "Service_Version_update: ...
Jenber Nurye's user avatar
0 votes
3 answers
92 views

If-else Parameter substitution

I'm running a series of experiments with bash and want to store the log files in a directory whose name is based on the experiment configuration. Some items in the configuration are boolean (true/...
Green绿色's user avatar
4 votes
2 answers
443 views

SSH for loop: parameter passed to function captured in variable is not expanded

I have several servers which have several files with deploy date info I need to parse, and get a local copy of the files which are 2 months old or older. #!/bin/bash # split on new line not space # ...
Nickotine's user avatar
  • 489
1 vote
1 answer
41 views

Can someone explain this potential variable expansion issue when running smartctl from script vs directly in terminal?

I am trying to write a script that collects some info about disk health as a part of customising ShredOS. I cannot understand what is wrong with the following snippet. #get disk info disk_info=...
Akhil's user avatar
  • 113
0 votes
2 answers
270 views

bash var substitution inside json to use in curl

Team, I am unable to substitute values inside json(myjson) to use in curl command. any hint? JENKINS_USERNAME="svc-user" JENKINS_USER_TOKEN="xxxx" JENKINS_INSTANCE_FQDN="sham....
AhmFM's user avatar
  • 125
0 votes
2 answers
74 views

How do I declare and use a variable in crontab that will hold the name of the admin user account?

I want the crontab for root to be agnostic, i.e. I don't want to literally specify in it that the admin user is jim. That is why in my crontab for root I introduced the variable au. SHELL=/bin/bash ...
John Smith's user avatar
0 votes
1 answer
78 views

SSH's -t option - how do I remotely execute a complex command that includes pipes and variable expansion?

The following script is run by root and is meant to share two directories from jim's computer to that of oumaima. #!/usr/bin/bash au=$(echo "$(head -n 1 /etc/doas.conf)"|sed 's/permit//;s/...
John Smith's user avatar
0 votes
0 answers
158 views

makefile value of variable is a the name of another variable

In a makefile, the value of variable is a the name of another variable. How do I get it inside target? For example: my_variable := Hello target: @var_name="my_variable" ; \ result=$(...
Yevhen's user avatar
  • 1
0 votes
1 answer
56 views

How do I correctly define and interpolate this variable into my find command? [duplicate]

I wrote a Bash script, mscript.sh for managing my music files, a part of it creates a list of such files. all_exts=" -iname \"*.webm\" -o -iname \"*.mkv\" " #all_exts=' -...
John Smith's user avatar
-2 votes
2 answers
165 views

defining variables inside function

After some reading somewhere on this interwebs, I found that it was best practice to to the following when using the output of a command as a variable: FILE_CORE_NAME="$(/usr/bin/env basename $(/...
Kevin Huntly's user avatar
0 votes
0 answers
33 views

Substitute square bus in csh script

I have input list: adr[0] adr[1] xadr[0] I want my output is: adr[*] adr[*] xadr[*] #! /bin/csh -fv set list = "adr[0] adr[1] xadr[2]" foreach f (${list}) set f_tmp = `echo "$f&...
ltdh's user avatar
  • 1
0 votes
3 answers
111 views

How to substitute line containing character with text stored in variable with sed?

I have a file entry.txt with a text such as $ cat entry.txt >Main entry This is some text to try how to substitute the first line of the text. I would like to substitute the first line by ...
Gigiux's user avatar
  • 527
0 votes
2 answers
271 views

How to grep for the value of a variable inside a shell script?

I have 2 files: a file full of values I want to look for my source text file I wrote a short shell command to loop thru my list of values and grep against my source file. If it doesn't find the ...
Classified's user avatar
0 votes
3 answers
77 views

How to expand variable inside single or double qoutes

I have 2 systems and they have the same directory strcuture. I want to execute a couple of commands on local system and the same on remote system. I use the wildcards so when I have a new version of ...
DEKKER's user avatar
  • 968
0 votes
1 answer
139 views

${parameter:?word} expansion for array range

${parameter:?word} prints the given error message if a variable is unset: $ echo ${unset_var?badness} bash: unset_var: badness How to use this on an array? $ array=(a b c) $ echo "${array[@]:1:2}...
Tom Hale's user avatar
  • 31.2k
3 votes
1 answer
62 views

${parameter:?word} expansion: `\}` difference between bash and zsh

I'm trying to get a literal } in the output of the ${parameter:?word} expansion in zsh. In bash, it works as expected -- I can quote the } with a \: % bash $ echo "${unset_var_name?...
Tom Hale's user avatar
  • 31.2k
1 vote
3 answers
331 views

ksh parameter expansion: replace / with -

Say I have this: str="@test/string" echo $str @test/string echo ${str#@} test/string works as expected, but echo ${str//\//-} ksh: ${str//\//-}: bad substitution fails. (expected @test-...
Lee's user avatar
  • 519
0 votes
1 answer
57 views

Shell script: Using variables makes command fails ( substituting values of variables manually ; command works fine )

In a bash script: jenkins_folder=`cut -d "|" -f1 -s input.csv` jenkins_url='https://url.com:8181/jenkins/view/' echo "jenkins_folder : ${jenkins_folder}" for job in ...
Deepak Singhal's user avatar
0 votes
1 answer
139 views

rtorrent scripting help

rtorrent provides a nice script for moving downloads to a directory based on label or name https://rtorrent-docs.readthedocs.io/en/latest/use-cases.html#versatile-move i'm trying to configure the ...
austin's user avatar
  • 101
-3 votes
2 answers
815 views

Bash Script - Expand ESCAPED dollar-sign ($) into its Variable

My Problem I have this run.sh script: #!/bin/bash TODAY=$(date) FILE="my_file.txt.\${TODAY}" When I echo FILE I get this: echo ${FILE} Output: `my_file.txt.${TODAY}` But I want this: ...
Bog's user avatar
  • 1,034
0 votes
1 answer
74 views

unterminated s command with seed on special characters with sed [duplicate]

i have special characters in the content of $VALUE which I would to add in file ($FILE). When I run this cammand : sed -i "s|T_VALUE|$VALUE|" "$FILE" i have an error unterminated ...
akaliza's user avatar
  • 101
3 votes
2 answers
7k views

Bash variable substitution into yq command

Given a YAML file, example.yaml: node: sub_node: get_this: I'd like to get a variable containing get_this using Mike Farah's yq and the string sub_node yaml="$(cat example.yaml)" ...
Lee's user avatar
  • 519
1 vote
1 answer
2k views

curl not working with when a parameter is passed via a variable in shell script

The following command works when entered directly on the console: [root@server user]# curl -d 'parameter=170.22.16.14 192.10.2.24 - - [01/Jan/1900:00:00:00 -0300] "GET /files/notes.txt HTTP/1.1&...
user3637971's user avatar
0 votes
1 answer
25 views

Error in defining Variables

I am trying to follow this to replace a multiline text. But to do that I want to put some text like below into a variable. ```dataview TABLE WITHOUT ID page-no as no, link(file.name, topic) as name ...
zakadmin's user avatar
0 votes
3 answers
81 views

why doesn't A=\"\" grep $A behave like grep ""?

I am on Linux, using bash. $ A=\"\" $ grep $A nomatch "" "" <- (returned by grep) $ grep "" imatcheverything imatcheverything ...
Christopher Lee's user avatar
0 votes
3 answers
592 views

What does echo $$ do? [duplicate]

I was messing around with echo and was trying stuff like echo ~. When I was trying to use $ to substitute environment variables I accidentally tried echo $$. Surprisingly instead of an empty line I ...
nded23959's user avatar
0 votes
1 answer
157 views

Bash - for range no longer works as wanted when one of the ends is a variable [duplicate]

#!/bin/bash myfirstarray=(1 3 5 7 9 11) for i in {2..4} do for j in {1..${myfirstarray[$((i-1))]}} do echo ${j} done done In the code above the range of the ...
wing47299's user avatar
2 votes
1 answer
76 views

mailx bcc cc options null hanlde

I am using below command for sending email, it working only if all parameter supplied. Please advise how to handle null/blank variable. For example, if -c -b is not supplied then ignore. mailx -a ${...
Veera V's user avatar
  • 43
0 votes
1 answer
1k views

Prevent splitting command line argument at spaces when running in bash script [duplicate]

I'm trying to run a command in a bash variable, like this: cmd="python -m mymodule 'argument 123 456' argument2=32 argument3=234" $cmd It looks like it is splitting the command line ...
user1506145's user avatar
0 votes
0 answers
87 views

How can I use an already defined variable reference in definition of another variable in shell script/ [duplicate]

I am trying to do something like below : export a="ABC" # Works fine export b_"$a"="DEF" # Works fine Now how do I print or echo the new variable "b_$a" ?? I ...
Jaivardhan's user avatar
0 votes
1 answer
71 views

bash: different behaviour of script and in terminal (loop over files)

I'm trying to loop over files with having different searching conditions based on the folder which I used with a case statement. basically it's: #!/bin/bash case folder in "Testordner") ...
Caro's user avatar
  • 3
0 votes
2 answers
806 views

Export a value from input variable

I need to export the values from input. However it's failing in bash. Here's the code - echo "Please enter HOST :" read RDSHOST && export RDSHOST And my value is something like - ...
Devendra's user avatar
2 votes
2 answers
2k views

How does the tilde expansion work within a shell variable?

I came across something funny when testing my script. I can ls my directory from the shell manually if I run $ ls ~/db_backups/ test1 test2 $ However, if I assign a shell variable a dir location as ...
Classified's user avatar
3 votes
4 answers
719 views

Bash function inside function: How to interpolate certain variables from outer function

I'm trying to create a function that creates a function. I want to "hardcode" an argument in the inner function based on what is passed into the outer function, e.g. outer() { inner() { ...
Andrew Cheong's user avatar
0 votes
2 answers
2k views

Using grep and command line argument

I have a file f1 with contents: james john joe marie james joe don marie I want to output the occurrence of the command line arguments in each line using grep in a shell script s1.sh with ...
Jay931's user avatar
  • 1
0 votes
1 answer
243 views

How can I batch rename files by combining three variables and ignore file extensions?

I'm trying to create a script that renames a group of files. Normally they will be TIFs but sometimes an occasional oddball could be thrown in so I would like to rename the file and ignore the file ...
dave-e-gravy's user avatar
0 votes
2 answers
893 views

sed and variables-substitutions on an custom script

well i ask here because i have around 12 hours trying with examples on internet for put a replace with sed on a custom script. In the file a had a lots of lines but i want to replace values after &...
Inukaze's user avatar
  • 47
0 votes
1 answer
461 views

How is the match evaluated in parameter expansion in bash?

I read "Linux Bible 10th Edition", chapter 7: Writing Simple Shell Scripts, at 149 page, "Parameter expansion in bash" paragraph. ${var:-value} — If variable is unset or empty, ...
CoderDesu's user avatar
  • 145
0 votes
1 answer
97 views

Find with optional parenthesised arguments

How to add optional grouped argument lists to find? For example, using an array of base arguments that define files that will always be found, no matter what other variable conditions are specified. # ...
markling's user avatar
  • 213
0 votes
2 answers
24 views

Putting variables in cardano-wallet function in terminal

How to enable variables in terminal to be changeable, for example address, I would like to be able to run this function through for loop in terminal with different addresses.
Jyu Viole Grace's user avatar
0 votes
2 answers
37 views

When looking at subdirectories, skip one directory

I have a directory /u01/oracle/folders with these subfolders: [root@ricusesasctl01vm tax_receipts]# ls -ltr total 64 drwxr-xr-x 5 OICDev1 oic 4096 Mar 1 2021 Argentina drwxr-xr-x 5 OICDev1 oic 4096 ...
Indermohan Mokha's user avatar
0 votes
0 answers
19 views

Something in the file is messing with ZSH [duplicate]

I have a file with such lines: map_Kn Ellie_Caruncle_N.png I'm trying to extract field #2, and use it twice in a command like this, but it's giving me some problems. Here is a ZSH session: ~/Downloads/...
alecail's user avatar
  • 1,643
1 vote
1 answer
55 views

Using Pipes to Generate Variable for Execution

I have the following test case that I am trying to work with: countAlpha=10;bob=alpha; echo $(echo $bob | sed 's/[a-z]/\U&/; s/^/\$count/') $countAlpha Which produces the following: $countAlpha ...
thePinochleKid's user avatar
0 votes
0 answers
18 views

cd command to a variable containing a path with spaces gives different output than actually giving the path as an input [duplicate]

I have a directory named "test 1" and I create a variable dest="test\ 1" I want to navigate to the directory using the cd command, so I type cd $dest which gives the output as -...
klaus_03's user avatar

1
2 3 4 5
7