Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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)

1
vote
1answer
995 views

displaying the PID

I am to write a subscript (called echomyvar) that displays the PID of the process running the script and the value of the variable named myvar. In the text it has: $ cat echomyvar echo The PID of ...
0
votes
1answer
56 views

How can i permanently change the PATH var of newly created user? [closed]

I assume this question has be asked many times.I just can't manage to change the PATH var permanently. This is what I do: nano ~/.profile export PATH=$PATH:/my/additional/path save changes I even ...
1
vote
2answers
118 views

How to pass a variable in a command in UNIX?

I am a completely new user to UNIX shell programming. I want to know how I can pass a variable that is now a comment to another comment which is for getting a file from a website. For example: I have ...
2
votes
3answers
571 views

Function to conditionally set a variable read-only

If I had a script which sets variables read-only to some odd values, and sets errexit because of other unsafe operations: #!/bin/bash set -e declare -r NOTIFY=$(case "$OS" in (macosx) echo ...
33
votes
2answers
32k views

In bash scripting, what's the different between declare and a normal variable?

In bash scripting: we create variable by just naming it: abc=ok or we can use declare declare abc=ok what's the difference? and why does bash make so many ways to create a variable?
5
votes
2answers
3k views

When should one use $( ) in defining variables

Some shell scripts I have come across use the following syntax when defining variables: file_list_1=$(echo "list.txt") or file_list_2=$(find ./) I would have used: file_list_1="list.txt" and ...
0
votes
1answer
141 views

please explain below command

getDateFormat() { year=`echo $1 | awk -F"-" '{print $2}'` mm=`echo $1 | awk -F"-" '{print $3}'` i=1 for x in January February march april may july jun august September October November ...
1
vote
2answers
268 views

about dollar sign: dollar sign within dollar sign?

Suppose we have: echo $A abc echo $B def echo $abcdef yesyes How do I get "yesyes" using A and B? I was trying something like: ${$A$B} $`echo "$A"$B` but failed. Any advise?
1
vote
2answers
49 views

Bash array with folder paths and wildcards

Given the above folder paths, I am trying to write a script that will delete files older than a certain amount of days. I am trying to use some more advanced techniques than just hard coding the ...
-1
votes
1answer
45 views

Bash: I have a variable in bash script; [duplicate]

I have a variable in bash script dirs= "4"; I want to get this number from another file so I am using a command dirs= sed -n '1p' < test.txt; It gets me the number, I see it in the terminal. But ...
3
votes
3answers
2k views

Capture the output of a shell function without a subshell

I have rbenv (ruby version manager) installed on machine and it works like that: $ rbenv local 2.3.1 Writing to stdout the local version of my ruby. I want to rescue this version and declare it in a ...
3
votes
2answers
331 views

Listing shell variables with a fixed prefix

I've looked this up and can't find an answer, I apologise in advance if it's been previously asked. I'm using shell on FreeBSD (/bin/sh) and I want to dump to stdout all shell (not environment) ...
3
votes
4answers
150 views

Variable output doesn't concatenate

I have a script that concatenates output from two different variables. The issue is that the output of both variables contains multiple rows. So, the output isn't what I expect. First variable and ...
2
votes
3answers
2k views

Bash script to fill in a template

I have looked for this elsewhere but not able to find something that matches my requirements. The template below is the one I need to use : dn: uid=$1,ou=home,dc=chan,dc=com objectClass: ...
0
votes
2answers
44 views

What is a “name reference” variable-attribute? [duplicate]

After reading this answer by Kusalananda I got some sense about what is a variable-attribute, yet I miss what is a "name-reference" (type?) of a variable-attribute, what is it using for, why would one ...
2
votes
0answers
33 views

MediaWiki: How to totally prevent anonymous users from register? [migrated]

I've installed MediaWiki aiming to create some one-man-editing Wiki (anyone can read it). I went through the documentation but I found no variable that controls whether anonymous users can create an ...
-1
votes
2answers
49 views

The need for $1 and $2 for comparison with an here-string

This is a follow up to this question; I don't know why but I keep misunderstanding the following code, although I try very hard to understand it: function read_and_verify { read -p "$1:" tmp1 ...
0
votes
2answers
969 views

Choose random string from a few and set it as a variable?

I have a few strings, and I want to set a variable to one of them, randomly. Say the strings are test001, test002, test003 and test004. If I set it like normal I'd obviously do it like this: test=...
1
vote
1answer
21 views

How do I capture this mysql output message in a variable?

I have this line in my script: mysqlinsert=$(/Applications/MAMP/Library/bin/mysql --host=localhost -uroot -proot --password=notmyrealpassword << EOF $mysqlrun) Despite what it returns, the ...
32
votes
3answers
124k views

Test if a string contains a substring

I have the code file="JetConst_reco_allconst_4j2t.png" if [[ $file == *_gen_* ]]; then echo "True" else echo "False" fi I test if file contains "gen". The output is "False". Nice! The ...
0
votes
2answers
48 views

here-document basename conflicts?

I am using EOF to generate bash scripts that run the Rscripts. In the Rscript I used basename to specify the output file name. When I use EOF to generate a list of bash scripts, I could not get ...
2
votes
2answers
66 views

Setting variable output from timeout

I'm trying to automatically query a status via telnet (this isn't something I'm able to work around in this case). The idea is to grep for something and assign the result to a variable, to pass to a ...
0
votes
1answer
59 views

Why value of a is 1?

I'm learning shell scripting and I am writing a script which lists the file for which the user has "7" permission, i.e. "rwx". So far I've written this: for F in * do a= $(stat -c "%a" "$F") ...
3
votes
3answers
111 views

tee into different variables

From the bash code command1 | tee >(command2) | command3 I want to capture the output of command2 in var2 and the output of command3 in var3. command1 is I/O-bound and the other commands are ...
0
votes
2answers
50 views

Linux ECHO output of a ECHO variable [duplicate]

I have a variable (var) which return value as VAR1 ($var has the value VAR1). There is an input file which has VAR1 defined (VAR1=ABCDEF) How can I use echo to get the value ABCDEF using $var?? I ...
0
votes
3answers
70 views

How to break a long string into multiple lines assigned to a variable in linux bash script

I am working towards writing a bash script that contains a variable with a long string value. When I split the string into multiple lines it is throwing error. How to split the string into multiple ...
8
votes
2answers
2k views

Use a shell variable in awk

Here is my script (to find the files that contain a specified pattern): find . -type f \ -exec awk -v vawk="$1" '/'"$vawk"'/ {c++} c>0 { print ARGV[1]; exit 0 } END { if (! c) {exit 1}}' \{\} \...
0
votes
2answers
31 views

Finding exact string from variable in an awk commmand?

I'm trying to the use the following awk command to return a user from a file that lists all users on the system but only if the 8th field is empty and they match the user variable (it's being using in ...
1
vote
1answer
4k views

bash add property using variable with jq

To add a property to a JSON text I manage to use result=$(jq -c '.docs[0] + { "value": "someValue" }'<<<"$fileContent") But if I want to replace "someValue" by a variable: result=$(jq -c '....
0
votes
3answers
34 views

Concatenating all arguments of a function/removing all spaces from a variable

What's a good way to concatenate all arguments of a function? What I got at the moment is iterating over them and appending them consecutively: func(){ concat= for arg in "$@" do concat=$concat$...
55
votes
5answers
82k views

Pass shell variable as a /pattern/ to awk

Having the following in one of my shell functions: function _process () { awk -v l="$line" ' BEGIN {p=0} /'"$1"'/ {p=1} END{ if(p) print l >> "outfile.txt" } ' } , so when called as ...
0
votes
4answers
49 views

Bash: while loop over numbered variables

My environment can have a flexible number of variables defined with the following format: Var1Value=A Var2Value=B Var3Value=C I now would like to iterate over the variables as long as they exists. ...
16
votes
4answers
15k views

dirname and basename vs parameter expansion

Is there any objective reason to prefer one form to the other? Performance, reliability, portability? filename=/some/long/path/to/a_file parentdir_v1="${filename%/*}" parentdir_v2="$(dirname "$...
8
votes
2answers
2k views

Indirectly expand variables in shell

I need to indirectly reference a variable in the bash shell. I basically want to what you can do in make by writing $($(var)). I have tried using ${$var} which would be the most straight forward ...
1
vote
1answer
110 views

How to populate many config files with same value(s) [closed]

I have many programs and all of them have some identical values in their config files (most, if not all, of which are in /etc). Let's say it is hostname, which is stored in the config files of Apache,...
0
votes
4answers
2k views

Replace an XML attribute's value with the value of a shell variable

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE POWERMART SYSTEM "powrmart.dtd"> <POWERMART CREATION_DATE="12/01/2016 17:43:15" REPOSITORY_VERSION="184.93"> <REPOSITORY NAME="...
2
votes
2answers
1k views

Using a Counter Loop for Environment Variable

I'm trying to write a code that moves files to a .junk directory using the built-in environment variables. My goal is that all a user would have to do is type sh junk filename1 filename2 filename3... ...
1
vote
1answer
140 views

Passing arguments from one script to another

Let's say I am in a Bash script and am sourcing a file. How do I pass variables to another script after I source the file like this. sed -i 's/ = /=/' $file source $file Let's say file contains ...
0
votes
0answers
17 views

Add locale in variable for command [duplicate]

I'm trying to call a command from a variable in a script with a specific locale, example : $ myYTDL="LANG=C.UTF-8 youtube-dl --get-filename" but each time I try and call it in the script, I get : $ ...
1
vote
2answers
113 views

MQTT as a variable on Bash?

Hi I want to try and convert a message receieved from MQTT into a variable so I can do an "IF script" using messages sent from a certain topic on mqtt. I tried this at top of script but didn't work: ...
3
votes
2answers
143 views

Setting variable with default value behaves differently when preceding a command?

Setting a variable with a fallback default value works...as long as we're using the variable in a subsequent command: $ unset APP_ENV $ echo $APP_ENV $ APP_ENV="${APP_ENV:-production}"; echo $...
10
votes
2answers
2k views

Can you explain these three things in this bash code for me?

I have a function in my .bashrc file. I know what it does, it steps up X many directories with cd Here it is: up() { local d="" limit=$1 for ((i=1 ; i <= limit ; i++)) do ...
2
votes
3answers
1k views

Shell: how to use 2 variables with for condition

I need to use 2 variables with for condition. For example, cat days 01072017 02072017 03072017 cat hours 00:00 01:00 02:00 03:00 my shell script sample for i in `cat days` & j in `cat hours` ...
1
vote
1answer
54 views

cd to a path containing spaces using a path variable [duplicate]

I have a file containing a list of commands which I run from my .bash_profile as source my_cmds For eg: $cat my_cmds scripts=~/scripts assuming my $HOME is /Users/knayak, when I run cd $scripts ...
0
votes
3answers
678 views

Returning local values from Bash variables?

I studied this article called Returning Values from Bash Functions. Data Lorem. \begin{document} hello \end{document} Case #1 which does not work Code #!/bin/bash function getStart { ...
0
votes
1answer
11 views

Length variable in netcdf file

I am trying to know the length of a variable 'i' in a netcdf file '122009.nc'. This is my script so far: #!bin/bash var=ncdump -v i 122009.nc var_length=${#var}
1
vote
1answer
140 views

Shell Script: Decrypt the encrypted password and store it in variable

I have a file "credential.txt" which contains the password of mysql. I have another script which calls this text file and retrieves the password from it. For security purposes, I don't want to store ...
0
votes
3answers
83 views

How to prevent parameter expansion around a variable I want to be resolved?

Edit: Important note before reading further: since Kusalananda's anwser, my question is now quite useless, since he pointed out a bug in another script I used. Now this bug is fixed, the below errors ...
1
vote
1answer
33 views

Does one string contain another in fish shell?

I am working on my fish.config for using the fish shell. I am trying to compare strings using bash syntax but fish doesn't accept the syntax. There is clearly another way to do it. Any suggestions ...
0
votes
2answers
230 views

Bash-Script: How to insert Variables into Bash-Script?

The following bash script is working completely fine: #!/bin/bash echo '!PaSsWoRd!' | openconnect --csd-wrapper=/home/user/.cisco/csd-wrapper.sh --authenticate --user=abcde123 --authgroup="tunnel My ...