Stack Exchange Network

Stack Exchange network consists of 174 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)

3
votes
2answers
41 views

A Unix AIX variable without a $ symbol?

I'm trying to debug an existing program and found an if condition without $ symbol prefixed to it. Values are: dt_val=1234 prev_dt_val=1234 If condition goes like: if [ dt_val -eq prev_dt_val ] ...
0
votes
1answer
15 views

Is this a bug in fsarchiver --version

I learned that in order to capture the output of a Unix command one should use something like RESULT=$(command in question) I want to capture the version of fsarchiver to the variable RESULT because ...
1
vote
1answer
24 views

Define variable using read from stdin of heredoc

I am trying to store Available and Total Memory into variables in a script file as follows, read -r Available Total <<EOT $(free -m | awk '/^Mem/{print $7; print $2;}') EOT $ echo $Total $ ...
0
votes
1answer
22 views

Bash: Error in assigning default value to a variable

Inside my bash script: This works: CWD="${1:-${PWD}}" But, if I replace it with: CWD="${1:=${PWD}}" I get the following error line #: $1: cannot assign in this way Why I can't assign to ${1}?
1
vote
0answers
22 views

Correct way to quote variables for use in Bash backticks? [duplicate]

I have a script which I want to go over 4 sets of files PURO*.xml, PURR*.xml, SALO*.xml and SALR*.xml. for j in PUR SAL do mask=("${j}O*.xml" "${j}R*.xml") for k in 0 1 do ...
9
votes
3answers
583 views

“Variabilize” the ampersand (background a process)

I want to know if there's a way to put the ampersand in a variable and still use it to send a process to the background. This works: BCKGRND=yes if [ "$BCKGRND" = "yes" ]; then sleep 5 & ...
2
votes
1answer
303 views

What is the purpose of BASH_ARGV0 in bash?

In the release for Bash-5.0 it is mentioned that they added BASH_ARGV0: a new variable that expands to $0 and sets $0 on assignment. What is the purpose of this? When is this an improvement over ...
0
votes
1answer
71 views

awk is not working for my requirement [duplicate]

My requirement is that shell script should print the folder name of the file. For example comp.class is present in D:/Practice/HelloWorldPractice/bin folder, I should get output after executing ...
1
vote
1answer
42 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: ...
2
votes
1answer
17 views

Ambigious redirect error on different volumes with mysql

i want to import a database with mysql (running with MAMP): #! /bin/bash # variables currdir=$PWD dbname=TEST dbuser=root dbpass=root dbpath="/applications/MAMP/Library/bin" exname=database if [ "$1"...
0
votes
2answers
63 views

Finding the Percentage from a grep variable

I want to display the calculation of the CPU load in percentage. num2=$(uptime | grep "average:" | tr -d " " | cut -d ':' -f 5 | cut -d ',' -f 2) percent=$((num2(*100))) echo 'CPU percentage' $...
1
vote
1answer
56 views

Remove all lines that contain $PWD

I have a file that contains paths - looks like this: /Users/a/Desktop /Users/a/Documents /Users/a/Documents/Work What would be the easiest way to remove all lines that contain the current directory (...
5
votes
2answers
274 views

Subtract time using bash?

Is it possible to use bash to subtract variables containing 24-hour time? #!/bin/bash var1="23:30" # 11:30pm var2="20:00" # 08:00pm echo "$(expr $var1 - $var2)" Running it produces the following ...
4
votes
2answers
108 views

What's the purpose of adding a prefix on both sides of a shell variable comparison to a string literal?

I have encountered comparisions of variables to string literals multiple times over the years which had one character prefixing the variable and the literal, e.g. if [ "x$A" = "xtrue" ]; then in ...
0
votes
1answer
44 views

how to get several commands into one variable for ease of use

I am writing a script and I noticed that a certain line of code is constantly being reused. So I thought why not put it into a variable for ease of use, and when something changes, I only need to ...
10
votes
2answers
548 views

Why can I not use variables as prefix to a command to set environment variables?

Normally, it is possible to set an environment variable for a command by prefixing it like so: hello=hi bash -c 'echo $hello' I also know that we can use a variable to substitute any part of a ...
0
votes
0answers
33 views

Complex nested for loop in directory

I have two sets of files in two separate directories organized as such: #first directory /direc1/files file0.num1.tar.gz file0.num2.tar.gz file0.num3.tar.gz file0.num4.tar.gz . . . ...
0
votes
1answer
67 views

How to strip out white space of a filename [on hold]

Opering System : RHEL 7 Bash Script: V_No=10.8.3 BUILD_VERSION=`echo ${V_No} | tr ' ' '_'` echo "File Name: Dev-${BUILD_VERSION}.jar" output: File Name: Dev-10.8.3 .jar expected output: File ...
0
votes
4answers
145 views

Temporarily declare a variable in Bash

To declare a variable in Bash, say in a Bash script-file (that doesn't include Bash function), I do for example x=y and when I finish using it inside that script-file I do unset x. Is there a way (...
0
votes
1answer
22 views

Why does read from variable give blank new variable? [closed]

I am trying to get a folder name from a stored variable string. When I ran the following path="Folder%20Name/Dir/File" read -d "/" folder < <(echo ${path/\%20/ }) echo "$folder" I am getting a ...
0
votes
3answers
46 views

split variable into multiple variables in loop

i am searching on these forums and trying different codes, but i cant seem to get it working: i have a variable ip="192.168.1.227 192.168.1.205 192.168.1.217" no i want to split every ip address up ...
3
votes
1answer
139 views

Bash string range and replace

To print part of the string, replace , with . I use command: echo "${q:16:6}" | sed 's/,/./' Is it possible to use something like: echo "${q:16:6/,/.}" because it does not work?
1
vote
4answers
58 views

Add Space Before Last Character in Variable

I have a variable that contains some numbers then a letter example: 242M I need to add a space right before the last character in that variable so that when echo'd it would look like 242 M I've ...
-1
votes
2answers
45 views

How to trim the spaces from first and last of the variable string in Linux? [closed]

I am trying to remove white spaces from each variable string. I tried with below command but no use. a="HARISH , SAM" echo $a|sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
-1
votes
1answer
83 views

Why does this for loop ignore my variable? [duplicate]

I am trying to do a simple script which runs a few commands N number of times, determined by the user's input. However, when it comes to run the commands (in a for loop) - The variable is ignored: ...
3
votes
2answers
333 views

I wish to print out the first two characters from $TEXT

I want my program to print out the text that I typed followed by the first two characters of that text. Is there any way to do this? #!/bin/bash # # Get text from user repeatedly # echo "Type away..."...
2
votes
2answers
167 views

How to filter a string against the characters from a variable?

I am trying to remove special characters (by passing them as a variable) from a string variable below command able to remove single characters not multiple? string=#@$AAA%* a=#$@%* #...
3
votes
1answer
142 views

grep: Not a recognized flag: o

I am trying to extract the date and timestamps from my log file string ($Data in the below code) on AIX using a regex and loading it into a text file as below: Data="Logs/2018-12-03/log.txt:3:2018-12-...
0
votes
1answer
53 views

BASH script: declare a variable inside CASE statement to use globally

case $location in ColocationOne) # Define MOSES LAKE workerarray server[0]=serverone server[1]=servertwo esac echo ${server[0]} How do I get this to echo: serverone ?
3
votes
2answers
41 views

Why isn't $foo=* results in an error?

I've learned that the shell expands certain kinds of characters. That's why when writing: $ echo * I get a list of directories and files, let's say I get: Applications Desktop Documents now, Let's ...
0
votes
1answer
31 views

Variable not getting resolved inside $()

I am expecting that variable url will be resolved in $() but I am getting variable of variable as url and not "http://testing.com" url="http://testing.com" echo "Fetching from ${url}" some_var=$(...
1
vote
2answers
166 views

elif condition in an if statement

I have this variables start=$1; end=$2; sn=${#start} en=${#end} and this if : if ( [ $# -eq 2 ] ) then elif ( [ $sn -ne 3 ] && [ -n "$(printf '%s\n' "$start" | sed 's/[0-9]//g') " ] ); ...
-3
votes
1answer
74 views

How to Increment Variables dynamically

How to increment variables dynamically. I have written below script but it is giving an error. #!/bin/bash X='101 Hari BAN' for i in "${X[@]}" do "V"$j=`echo $i|cut -d' ' -f$j` ...
0
votes
0answers
16 views

shell - mysqldump works with echo but not directly

Im preparing a shell script which will take mysql table dump with where clause. Im passing table name and where condition from a parameter. Backup command: mysqldump -u root-p'pass' db ...
0
votes
3answers
56 views

GREP for pattern and remove all the junk characters before or after the pattern

I have a set of files in a directory. And every file will have a line called ---PUBG-xxxxx-- or ---PUBG-xxxxx, PUBG-yyyyy ----. Below is the output of the grep command. grep "^--" FILE*.sql | grep "...
5
votes
1answer
524 views

How to distinguish between a positional parameter and an option?

Both a positional parameter ($1, $2, and so forth) and an option (and/or argument) are both written directly after a command, so what is the definition or phrasing to explain how to distinct them? In ...
10
votes
3answers
682 views

How to POSIX-ly count the number of lines in a string variable?

I know I can do this in Bash: wc -l <<< "${string_variable}" Basically, everything I found involved <<< Bash operator. But in POSIX shell, <<< is undefined, and I have ...
0
votes
1answer
43 views

C-Shell : variable in variable

I want the value of variabe ${var}_err_num, but i can't do it. #!/bin/csh -f set list = "book bus apple" foeach var $list set err_num * * @err_num = $err_num + 1 set ${var}_err_num = $...
2
votes
1answer
75 views

Bash: Use raw binary in variable as executable?

I'd like to create a variable containing binary data, then execute in a command. Example: #!/bin/bash var="$(cat /bin/sleep)" "$var" 9999
0
votes
1answer
85 views

Why can't I use “$whoami” as a variable in “chown $whoami path”?

Why doesn't this work? [my_user@archlinux ~]$ sudo chown -R ${whoami} /my_folder/path1/path2 chown: missing operand after ‘/my_folder/path1/path2’ Try 'chown --help' for more information. [my_user@...
-1
votes
0answers
16 views

SetEnvIf variable scope

does setting an variable with the SetEnvIf directive applies to the whole server or just the given <Location></Location>where it is being placed?
0
votes
2answers
61 views

How can I wrap this checking of variable set/unset into a function?

As https://stackoverflow.com/a/13864829/ said, $ if [ -z ${aaa+x} ]; then echo "aaa is unset"; else echo "aaa is set"; fi aaa is unset can test if a variable aaa is set or unset. How can I wrap ...
1
vote
2answers
136 views

How can I use a bash variable as a regex argument to gsub() and match()?

I have a script #! /bin/bash awk -v pat="$1" '{ if(match($0, pat)) {print $0} }' "$3" awk -v pat="$1" -v rep="$2" '{gsub(pat, rep); print $0}' "$3" I would like to run it like myscript '\.\.+' ' ' ...
1
vote
2answers
62 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 ...
-1
votes
0answers
21 views

Apache CGI and variables [duplicate]

Who is responsible to passing information from a client request to CGI variables (Apache?). We use IBM Tivoli Access Manager and it basically pre-authenticates all users and sets a variable in the ...
-1
votes
1answer
121 views

basename extra operand error [duplicate]

I have a script that will try to extract the file's base name and then I do additional work with it. Only when using this script with a file with this naming convention (including spaces and ...
0
votes
2answers
45 views

Bash - How to put an array inside values of variable?

I have a web.txt file which contains: youtube facebook google My sh file contains: readarray web < web.txt url = www.website.com Commands I have used: readarray web < web.txt url = www."$...
0
votes
2answers
59 views

Append strings to multiple space-separated values in a variable

I have a variable VAR="XYZ YZA ZAB" declared and I would like to append some strings next to all the words in that variable. For example: I want to append .file1 to each space-separate substring in ...
1
vote
1answer
49 views

bind variable and shell scripting with sqlplus [closed]

I wish to execute DMLs using bind variable with shell scripts. For example, something like: #!/bin/bash SH_NUM=10 sqlplus -S test_user/test_pass <<EOD var a number; a:=${SH_NUM} insert into ...
1
vote
1answer
70 views

Using variable instead of a cat output [closed]

In a shell script how can I get the same output from a variable for piping like a command does (for example: cat data.dat | awk ...)? list=`cat data.dat` then update list with running it through awk....