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
votes
0answers
20 views

using case statement to read from a variable/file while creating a MENU

read -p 'enter your choice [ 1-5 ]' choice case $choice in 1)abcrequest 2)defresponse 3)afgRequestReply 4)FSfReply 5)ghjrequest I want the case ...
0
votes
1answer
33 views

echo return an empty variable $

I have this command echo "<?php $x=$_GET['x']; echo $x;?>" > x.php the output file x.php is: <?php =['x']; echo ;?> Has that any relation to the $ variable? and how do I avoid ???, ...
2
votes
1answer
57 views

How to render variables without quotes in bash script?

The following script: $ cat runme01.sh #!/bin/bash A=myval B=$A/{fix} C=$A/fix set -xT echo $B echo $C prints the following if ran: $ ./runme01.sh + echo 'myval/{fix}' myval/{fix} + echo myval/...
2
votes
2answers
44 views

Linux Bash: Variable to another variable [duplicate]

i have this: echo $MSG | sed -e $'s/;/\\\n/g' I want to put the result of that sed in a new variable called $MSG2 Something like: $MSG2=echo $MSG|sed -e $'s/;/\\\n/g' How can i do it? Thank you!...
0
votes
2answers
38 views

Cannot escape '?' character in variables (unix)

I have been looking for this question everywhere, but I didn't find it and there doesn't seem to be any kind of answer yet to my current problem, which, frankly, doesn't even make sense. So, what I ...
1
vote
1answer
33 views

Inconsistent behavior of bash string replacement

When I print a variable with one substring replacing another as described in this documentation, it behaves correctly: stringZ='abc - 123' echo $stringZ # abc - 123 echo ${stringZ/ - /...
0
votes
1answer
28 views

How to assign a zero-length, non-null string

Based on this, amongst many other things I've read, my understanding is that a='' makes a both null and zero-length. But, then, how does one create a zero-length, non-null string? Or, is there no ...
0
votes
1answer
35 views

Translate a single line into a file with one shell command

I'm looking for a convenient command to run from my terminal. At now I'm using $> FILE=./file_for_translate.md && ROW=84 && \ DATA=$(sed -n ''"$ROW"'p' $FILE | trans -no-init -no-...
1
vote
2answers
32 views

Bash Perl multiline match into variable

I am trying to extract 2 strings from a multiline text. postmsg="\033[0;37m Email Settings" This line finds some match at the beginning of the string and puts it in a variable inCol. inCol=$(perl -...
0
votes
3answers
16 views

Writing a variable into a container's file at a certain line

I need to write a variable into a file which is in a docker container at line 31. I use a bash script my command is: txt=" log-bin server_id=1" docker exec $container sed -i "31i\ $txt /etc/mysql/...
2
votes
4answers
155 views

Convert only certain words to lowercase

Say I have a variable var whose value is fOo bar1 baR2 bArab. How do I go about saving into another variable, say, lc_var, a version of var where all but the first word are converted to lowercase? I ...
0
votes
2answers
47 views

Sed escaping “.” and use $variable?

I don't understand this escaping. Is some generator available for it? i need replace normal text (185...) with Variable $NEW_DNS sed -i "185.228.168.168,185.228.169.168|$NEW_DNS /etc/wireguard/wg0....
1
vote
1answer
19 views

Adding a NEW LINE character in between variables

How can I add a new line character (\n) between two Unix variables? I tried the following but it's getting printed in a single line instead of separate lines. h=hello w=world c="$h"$'\n'"$w" echo $c ...
2
votes
1answer
34 views

Why doesn't pattern substitution work from a single variable?

Please see the below session transcript: $ mkdir temp $ cd temp/ $ touch file{1..5} $ ls file1 file2 file3 file4 file5 $ SRC=file; TGT=ram $ for f in file* ; do mv $f ${f/$SRC/$TGT} ; done $ ls ...
2
votes
1answer
80 views

Shell script returning “Running: command not found”. Not sure why

When I am setting a variable to a command, I am getting the answer along with the error "command not found". Below is the command in the script. #!/bin/bash fs=$(ps -C "daemon_name" > /dev/null &...
2
votes
2answers
35 views

Regex to check arguments of command

I want to use grep to check if a command called cmd that was ran, contains the word name as an argument. (I know about about !* in Bash, I don't want to use it). Let's say I have the command in a ...
-1
votes
1answer
27 views

How to Force String Type on a Variable?

I have this script: #!/bin/bash USERS=("user1.com" "user2.co.uk" "user3.com") PASSES=("pass1" "pass2" "pass3") for i in ${USERS[@]}; do echo ${PASSES[i]}; done running this script outputs: ...
7
votes
3answers
115 views

Can I duplicate output of a pipe?

Well, the task is simple: a part of my script has to compute both md5 and sha1 hashes. The input is a file - big file - and hashes have to be put into MD and SH variables for later output composition. ...
0
votes
1answer
32 views

Output of piped command to variable not working

When I try the following there is no problem: $ date '+%y%m' | sed 's/.\(.*\)/\1/' 906 But When I try to store it in a variable it seems nothing is stored: $ dd=`date '+%y%m' | sed 's/.\(.*\)/\1/'` ...
1
vote
2answers
69 views

What is a variable attribute?

I aim to understand the general concept of "variable attributes" hoping it will help me understand what is declare in Bash. What is a variable attribute? Why would someone want to give an attribute ...
0
votes
0answers
20 views

Assign temp file from cgi and read it in .sh [migrated]

I have .cgi file and .sh file. The cgi is calling the script which is the .sh file. How will assign the tempfile created from .cgi and pass it to the .sh file? Example below: (on my CGI, $tmpfile is ...
2
votes
1answer
57 views

Pass variable to cat [duplicate]

I have only one variable in bash script ${PHP_V} and trying to pass in nginx config file like: cat <<'EOF' > /etc/nginx/sites-available/default server { listen 80 default_server; ...
3
votes
2answers
82 views

Get value of the passed argument to script importing variables from another script

I am trying to export variables from one script to the main script and passing one of the imported variables as argument to the main script. Here is the script fruitcolour.sh having variables only : ...
0
votes
1answer
31 views

Variable within path to file in bash script

I've got a file, that contains about 20k strings. Each string in that file should be an argument for a command within bash script. At first my script generates a file with this strings. find /home/...
7
votes
1answer
199 views

Does `declare -a A` create an empty array `A` in Bash?

Does declare -a A create an empty array A in bash, or does it just set an attribute in case A is assigned to later? Consider this code: set -u declare -a A echo ${#A[*]} echo ${A[*]} A=() echo ${#A[*...
0
votes
2answers
110 views

Interpret variables from read in string with shell script [duplicate]

I have a text file which is usually filled with multiple lines which I want to "print" with a while loop. The text inside this file contains variables - my problem is, that these variables are not ...
0
votes
1answer
30 views

What happens when we run var=3 command [duplicate]

Below I run what I expected to be an invalid command: var=3 date, which in fact isn't. $ var=3 date Sun May 26 17:10:22 UTC 2019 $ echo $? 0 But the variable wasn't assigned the value 3: $ echo $...
1
vote
2answers
27 views

Bash Split Variable into 3 Variables based off Delimiters?

Trying to split a string var with the following pattern: TestString="Artical Title Here (ClassYear) [Author]" I was able to get the title into a Var using cut as such: ATH="$(cut -d'(' -f1 <&...
3
votes
0answers
48 views

When coding for Unix/Linux is it convention to use a trailing slash on path names, or to omit it?

When programming for Unix/Linux, is it best practice to use a trailing slash, or to omit it? For example: Trailing slash baseDir="/path/to/dir/" #code# outPath=${baseDir}addition/to/path/ No ...
0
votes
1answer
23 views

add a line to file that contains quotes and variable using bash

I want to add this line to /etc/grub.d/00_header set superusers="ubuntu" where ubuntu is the result of $USER. I've tried this: sudo bash -c 'echo "set superusers='"${USER}"'" >> /etc/grub.d/...
0
votes
0answers
13 views

How to call a R variable in a loop with Slurm? [migrated]

I have a R (RHO_COR.R) script and I would like to create a loop in order to split the jobs on several nodes. I show the part of the script where I would like to create the loop. res <- foreach(i =...
0
votes
3answers
614 views

Using `printf` to print variable containing `%` percent sign results in “bash: printf: `p': invalid format character”

I want to use printf to print a variable. It might be possible that this variable contains a % percent sign. Minimal example: $ TEST="contains % percent" $ echo "${TEST}" contains % percent $ printf ...
1
vote
0answers
19 views

How to clear DISPLAY variable in GNU screen

Basically, i need to unset DISPLAY variable in screen if I run multiple commands like: screen -dmS name bash -c 'kodi; unset DISPLAY; emulationstation' I'm writing a little something that will ...
4
votes
3answers
256 views

Iterate lines of string variable in bash

I have a script where I want to list USB devices using the command lsblk. The command: $ lsblk -o NAME,TRAN,VENDOR,MODEL | grep usb which results in sdb usb Kingston DataTraveler 2.0 sdc ...
0
votes
1answer
29 views

Evaluate command based on variable at bash [closed]

I am writing a small script that would choose random file for me from the directory and execute command with this chosen filename as a parameter. It is about choosing random .ovpn file and run sudo ...
0
votes
1answer
22 views

Why can't echo and printf output variables that have only whitespace characters? [duplicate]

var1=$(printf "\n\x0A\n") var2=$(printf "\na\nb\nc") Using Android Terminal, the output of echo -e "$var1" (same for echo and printf) iss nothing, not even the 3 new lines. But echo -e "$var2" or ...
0
votes
1answer
42 views

very long string variable?

Is there a longer type variable for building lengthy sql statements? how add new line too? I want to store the statement in a very long variable text so I can run it from sql prompt. I think it will ...
0
votes
1answer
28 views

awk save value as variable

I use the command varioutput=$(awk '{print $j}' OFS=, data/damper.test_temp1.csv) because I want to extract the j th value of a line of many values seperated by , . But when I want to use $...
0
votes
1answer
54 views

Running command in a variable [duplicate]

It is often said that variables should be double quoted when used. In the following example I want to have the command echo "You can't see me" in a variable and run it in such a way that the output ...
1
vote
3answers
78 views

If line contains pattern, print variable to column

I have the file DEMLIR-GEO_OPT-1-distance-1.coordLog: REQUESTED STRUCTURE DATA Distance vector r(i,j) between the atom i and j in ANGSTROM r(1,5) = 0.944776 0.190651 1....
0
votes
1answer
34 views

Length of a variable

I imported latitudes and longitudes from text files. These two textfiles contain a few thousand of numbers. I read them into variables, now I want to know just how many numbers are in these files. I ...
0
votes
1answer
22 views

Manually enter password into expect and use as variable while scrip is running

I have an expect script that uses interact every time a password is needed. The program that expect is automating asks for the password multiple times. Is there a way to only have to type the ...
0
votes
2answers
46 views

Bash String Manipulation and Assignment

How can I manipulate a string and then assign it to a variable? This string manipulation works as I want: echo ${dir:2:5} | sed 's/[.]$//'; but: var x = ${dir:2:5} | sed 's/[.]$//'; and x = ${...
1
vote
2answers
44 views

grep from list of keywords

Over the past couple of days I've been trying to dig through directories of files to report on files containing key words. Through the help of other users showing me the correct syntax for grep and ...
1
vote
2answers
97 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
47 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 ...
0
votes
2answers
61 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 ...
-1
votes
2answers
53 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 ...
1
vote
1answer
23 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 ...
0
votes
1answer
60 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") ...