Questions tagged [variable]

A variable is a name, if correctly chosen with a symbolic meaning, that holds a value or values. Use this tag if your question is specific on the use of variables on shell scripting (if you want to ask about variables in programming languages you should probably ask on StackOverflow)

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

How To Have GNU Parallel Command Pick up Variables, Defined Outside of It?

My actual case is more complicated, but this is the problem I'm having. Simplified Example echoSimultaneously.sh: echoSimultaneously() { parallel -j 0 echo ::: $string } run() { local IFS ...
Nickotine's user avatar
  • 467
0 votes
1 answer
59 views

Bash brace expansion doesn't work in an export statement

Consider the following example: $ echo file_{a,b,c} file_a file_b file_c # brace expansion worked :) $ export VARIABLE=file_{a,b,c} $ echo $VARIABLE file_c # brace ...
Thomas Fritz's user avatar
1 vote
1 answer
91 views

Why does -n with unquoted variable containing empty string return true? [duplicate]

From man bash: -n string True if the length of string is non‐zero. Examples: # expected $ var=""; [ -n "$var" ]; echo $? 1 # unexpected? $ var=""; [ -n $var ]; echo ...
pmor's user avatar
  • 579
5 votes
2 answers
712 views

Is it while loop or the pipe causing global variable behaving unexpectedly

Could please someone explain the (from my POV) strange behavior of the COUNTER variable in the following code? #!/bin/bash COUNTER=0 function increment { ((COUNTER++)) } function report { ...
Jozef Chocholacek's user avatar
1 vote
0 answers
38 views

Why does `$PS1` sometimes include the bell, `\a`? [duplicate]

In Ubuntu 18.04 and 22.04 (WSL), I've noticed that $PS1 contains a "bell" character, \a: $ echo $PS1 \[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\...
AJM's user avatar
  • 133
-1 votes
1 answer
62 views

Is the following `readonly` use POSIX-ly correct?

I defined the following as read-only: readonly root_command='sudo -s' later used in my script as in: exec $root_command My question is, maybe I am slow or something, but I do not fully understand ...
Vlastimil Burián's user avatar
0 votes
1 answer
81 views

How to assign null to or unassign a Bash variable if parameter is set?

Variable USE_TLS is to be unset, null, or empty when TLS is not to be used, but set to 1 when TLS must be enforced. I require it to be enforced by default (i.e., set), but not if I specify another ...
Code Maverick's user avatar
0 votes
0 answers
48 views

I want to issue "sudo su - <user>" to create a login shell, and pass an environment variable into the new shell [duplicate]

Oracle linux 7.9. Shell is bash. I log in as user bob. From there I sudo to user sam and do my work. But I want to pass a variable into sam's new login shell. So for example, this is what I currently ...
Andrew Grady's user avatar
1 vote
0 answers
71 views

diff two multilines bash variables to list what lines aren't in the other

I've created a bash script that Lists the document IDs I've put in an Elastic index, in $liste_ids_lines: a004-événements-examen_individus_localisés_ou_non a020-les_points-leurs_indicateurs-...
Marc Le Bihan's user avatar
1 vote
2 answers
103 views

Redirect to a filename that will be a variable and contain a command substitution

I'm using nmap to scan a network and then create an XML file that contains the scanned information. I want that XML file to contain the date of the scan so that each file is unique and I can see the ...
johnmacward's user avatar
1 vote
1 answer
174 views

Change hostname with a variable with for loop

I am trying to change the "hostname --transient" to some computers with a for loop using a variable. I want it to collect the "hostname -f" from the server itself and, using the ...
henrolos's user avatar
0 votes
1 answer
93 views

Using awk -v with Shell variable works great and prints to terminal, but when redirect to a file only the 1st record prints to the file

I am reading a file with a matching pattern then getting the first five lines after the matching pattern. The patterns are not in proper numeric order, so I am using a numeric variable in the pattern ...
Joey Simon's user avatar
1 vote
1 answer
159 views

some_variable vs some-variable in zsh

Why is it that zsh seems to understand what to do with some_variable=1 command, but not some-variable=1 command? E.g.: $ fs_screen=1 mpv --fs-screen=${fs_screen} someFile.mp4 works fine but $ fs-...
Harv's user avatar
  • 2,014
0 votes
1 answer
158 views

bash: string variable contains asterisk. how to use this variable for searching etc with grep, sed?

Have a nice day I have got text file (zz.txt): Chemical name 3-Aminopropane-1-sulphonic acid Synonym(s) -- Homotaurine * Tramiprosate -- Chemical name Common name and synonyms ... I have variable s=&...
Alex Den's user avatar
0 votes
1 answer
51 views

Bash script variable syntax: with some commands it works, with others it does not [duplicate]

I do not understand the behaviour of this variable: SSH_CONFIG_FILE="~/.ssh/config" echo $SSH_CONFIG_FILE ls -l $SSH_CONFIG_FILE ls -l ~/.ssh/config This is the output: ~/.ssh/config ls: ...
Pietro's user avatar
  • 581
1 vote
3 answers
58 views

sed: update 2 similar variables in a file but keep the upper and lowercase

I have 2 variables in a file like below that I need to assign a new value inserted by one user input: read -p "Enter CName Name : " CName sid=C02SBX SID=C02SBX When I run the following ...
Ali's user avatar
  • 123
0 votes
2 answers
173 views

Use SED to replace part of a current variable with user input variable

I'm trying to replace only part of the existing variable with a new user input variable as below: #Current Variable: gdbName=Test.MY.DOMAIN.COM <--I need to replace the "Test" (This can ...
Ali's user avatar
  • 123
1 vote
0 answers
186 views

Extract from a curl its dire failure message (if case of return code != 0), status code, output, and error message into four different variables

I need to extract from a curl emitted: the bash error message coming with a non-zero ?$ return code, in case my command would direly fail the http status code returned by the server contacted the ...
Marc Le Bihan's user avatar
4 votes
2 answers
1k views

How do I reliably capture the output of `ls` in this script?

The following script, play_movie.sh is meant to automatically select a movie if there is only one in the current directory and play it with ffplay. Otherwise it is meant to display a selection of ...
John Smith's user avatar
0 votes
2 answers
208 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
69 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
  • 906
0 votes
1 answer
85 views

How to store command with option on variable of a function in zsh?

I'm trying to learn bash scripting using freeCodeCamp tutorial for beginners on YouTube. I'm stuck at the point where he shows how to create a function. He saved on a variable a command with an option ...
Paul Marcelin Bejan's user avatar
-4 votes
1 answer
51 views

How to protect variable name inside shell arithmetic?

Variables can be protected with curly braces. Mynewvar=1 echo $Mynewvar 1 echo ${Mynewvar} 1 However I can no longer protect them when inside shell arithmetic. Mynewvar=1 echo $((Mynewvar+9)) 10 echo ...
jwav629's user avatar
1 vote
0 answers
69 views

Can you assign a default array to a variable by specifying its values explicitly?

I know that I can use an existing array as the default value for a variable that is potentially uninitialized via default_values=(1 2 'value is a string') array=("${array[@]-${default_values[@]}}&...
da22e's user avatar
  • 11
-4 votes
2 answers
204 views

Is it possible for any shell to interpret a string with decimal point as a number (int, float)?

This idea came to me with recent updates to Visual Studio Code, in which I usually shellcheck my code automatically as well. This is what I see, so you can get the idea: This question is very simple ...
Vlastimil Burián's user avatar
0 votes
1 answer
144 views

How to assign output of Grep in a Variable

I am trying to assign the output of grep in a variable . The below command provide me the correct output in a script str="DWMessStage-20230609-083501.xml__a6e27c1.4a73a37b.13.1889d3efa14.N7f9d....
Shuvra Maitra's user avatar
1 vote
2 answers
174 views

Grep not working - searching a file with a variable

I am trying to use grep to find the lines in a file that start with a specific variable I am defining. I know that you need to use double quotes with this command when searching with a variable, but ...
marissa's user avatar
  • 75
-2 votes
3 answers
379 views

Bash Read First Line of File with Variable Assignment

Unity has a variable assignment in a file called "ProjectSettings/ProjectVersion.txt" but when I try to read it, it tries to run and throws an error. The first line of the file is ...
kirbycope's user avatar
0 votes
1 answer
1k views

Zsh i named variable; "bad math expression: operand expected at `/houdi/loo...'"

this's quite puzzling me, doing Zsh assignment using variable namedly i % for i in '/houdi/lookup' '/komando/' ;{ echo value i = "$i" } zsh: bad math expression: operand expected at `/houdi/...
user avatar
0 votes
0 answers
307 views

Including square brackets in variable values

In the following script #!/bin/bash shopt -s globstar set -e fwd_slsh='/' space=' ' ampersands='&&' bang='!' opnbrc='[' clsbrc=']' caret='^' prefix=${bang}${fwd_slsh}${opnbrc}${caret} suffix=...
Vinod's user avatar
  • 155
2 votes
2 answers
104 views

Parse command with expanded variables

Trying to test something and not quite nailing it either way I've tried, so not sure if I'm being silly or not. Essentially I need to parse a string to ipa-getcert's post-save argument which should ...
dmgeurts's user avatar
0 votes
1 answer
497 views

Add Asterisk to a path Variable?

I need to use *.* at the end of a Path that is stored in a variable. Also just one "*" should be OK as well. finally I want to move files in folders with same name. With a static Path like /...
Donaufisch's user avatar
1 vote
1 answer
199 views

Script to select a PulseAudio sink from an ordered list and use its name as command input

I wish to write a bash script that will create a PulseAudio loopback device between an audio source and a bluetooth device. (Context - skippable) So far I have (the relevant part of) my script as: ...
Marc.2377's user avatar
  • 1,112
0 votes
1 answer
609 views

Shell scripting, reading a list from tt file, use as variable, use the variable to run python scripts in a foor loop

I am trying to run a shell script where it reads the variables from a txt file, uses that variable in a for loop to run python file. The code reads a txt file where at least there are 100 variable ...
BircanA's user avatar
  • 21
0 votes
1 answer
356 views

How to use a path to a directory with a space within variable to cat all files within the directory?

I have a variable with path to folder1 containing a white space: TEST=path/with\ space/to/folder1/ I want to concatenate all files within a folder1 with cat but it gives an error: cat "$TEST&...
trilisser's user avatar
  • 103
1 vote
2 answers
2k views

Variable username in SSH config

I am looking for a way to parse a variable to the SSH config file. Currently I have a script that replaces the values before distribution. I'd rather eliminate this step. I've been over the manual ...
proxx's user avatar
  • 77
0 votes
1 answer
727 views

Can't capture the output of git pull command in a variable

I run git -C /another-directory pull and I get this error: Your configuration specifies to merge with the ref 'refs/heads/main' from the remote, but no such ref was fetched. Now I want to get that ...
Saeed Neamati's user avatar
1 vote
1 answer
64 views

Separate variable scope spawning commands under job control

Is is possible in bash - or other sh-derivative shell - to run in the foreground from command-line a list of commands that have their own variable scope (so any values assigned to variables in that ...
S P Arif Sahari Wibowo's user avatar
2 votes
1 answer
270 views

printf "%.3f" ${variable with newlines} - error with \n

Like HERE I have a file.csv with numbers in quotes: "0.2" "0.3339" "0.111111" To round the number (3 decimals) this solutions works great: printf "%.03f\n" $(...
R 9000's user avatar
  • 167
0 votes
1 answer
37 views

How to send expression with variables over ssh?

I can run the followind loop locally for i in A B C; do mycommand $i; done Now I am trying to send it over ssh The following version ssh myhost for i in A B C; do mycommand $i; done The following ...
Dims's user avatar
  • 3,255
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
1 answer
34 views

Dynamic variable references

I want to define a variable my_stacklist with the value for {mypc}_stacklist so if mypc=kast I get to access the content of kast_stacklist via my_stacklist. I thougt this was the same as my earlier ...
pvk's user avatar
  • 5
0 votes
3 answers
94 views

Combine .csv-files with different amount of lines

Here I asked already for a similar problem. Now I have 3 .csv-files but one of them has only 1 line. file1.csv dog cats mouse file2.csv 001a 002a 003c file3.csv WORD The output should be dog,001a,...
R 9000's user avatar
  • 167
0 votes
2 answers
162 views

How to get the Path difference and store it in a variable

Suppose I have two paths. Path1 which is static and path2 which is dynamic. Example: Path1= /tmp/folder/ Path2= /tmp/folder/dir1/dir1_2 Result: Diff= /dir/dir1_2 I want to achieve as above example....
Asif Ahmed's user avatar
0 votes
1 answer
31 views

variable contains $, expansion needed

The script fetches the path of a file via some command and then the mount command runs. pkgname= #defined here su -c "apkfile= #defined here &&\ stockapp=$(pm path $pkgname | sed -n '/...
decipher's user avatar
  • 302
0 votes
2 answers
48 views

How to pass commands around?

I have this simple script, which does nothing more than: check if an email matches a specific pattern in that case, add a tag to a taglist before quitting, print that taglist set -e lista_tag=() ...
gobbolo22's user avatar
0 votes
0 answers
190 views

Any variable assignment would be empty inside docker

I have a long script that would be copied inside the docker image in Dockerfile and would be executed there. I'm using bash. Both in my local machine, and inside my docker. I run this code to ensure ...
Saeed Neamati's user avatar
1 vote
1 answer
304 views

Environment variables not passed to the user

Even though I set ENVIRONMENT=production in /etc/environment and DefaultEnvironment=ENVIRONMENT=production in /etc/systemd/system.conf the variable ENVIRONMENT (which should be global by now) is not ...
manifestor's user avatar
  • 2,463
0 votes
2 answers
140 views

Use a variable value only for x times

I am making a bash script where I need to run a command using a specific ID. But a single ID can be used only 10 times. After that the ID should change For Example I have three IDs ID=(abcd1 abcd2 ...
ph3ro's user avatar
  • 378
1 vote
1 answer
214 views

How to use variables (that change in loop) in awk statement to cut a file

I have a large file that needs to be filtered by the first field (which is never repeated). Example as below: NC_056429.1_398 2 3 0.333333 0.333333 0.333333 0.941178 NC_056429.1_1199 2 ...
natasha15's user avatar

1
2 3 4 5
25