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)

1
vote
1answer
27 views

Makefile: Default Value of Variable that is set but has null value

I have a Makefile that has a variable that needs to have a default value in case when variable is unset or if set but has null value. How can I achieve this? I need this, as I invoke make inside a ...
0
votes
0answers
13 views

How to append a “/*” to the end of a variable with spaces and glob it? [duplicate]

If I have a directory wibble, the following command does what I want: WIBBLE=wibble wc $WIBBLE/* If I have a directory foo bar, the following command does what I want: wc foo\ bar/* But this doesn'...
0
votes
1answer
37 views

Python script runs as bash command on terminal but not as a cron job

I am trying to run a python script on my Raspberry Pi (Stretch OS) as a cron job but the script fails to run fully. On crontab -e, it reads like this: */1 * * * * python /home/pi/project/myfile.py &...
2
votes
2answers
60 views

Default variables in Bash

The default variable for loops in Perl is $_. Is there any equivalent of this in Bash?
1
vote
4answers
42 views

Get numeric value of cat output

When I save result of cat to the variable I get the following: wfpd=$'16213\r' How can I convert this result to numeric value in order to use it as an argument to another command
3
votes
1answer
214 views

Unable to redirect bash output to variable file name

I am trying to redirect the bash output to a variable file name. Here is my script looks like #!/bin/bash for i in `cat servers` do if [ "$i" = "198.162.1.3" ]; then var="apple" fi ssh test@$i "uname ...
1
vote
1answer
30 views

cron not executing command with variable

I have some logic for executing java projects; it all works in the terminal console when I type it, but not in the cron scheduler: run 1st microservice and get variable from POST request: java -jar /...
2
votes
2answers
55 views

bash script get result from test into variable

I'm trying to learn Bash scripting and for fun I would like to write a script which will monitor the httpd.service. The basic construct looks like this: #!/bin/bash SERVICE=httpd.service if [ "...
1
vote
4answers
57 views

Pass function argument to defined variable

How can I "inject" a function argument to a defined variable like in this example? mood="i am $1 happy" happy () { echo "$mood" } happy "very" Current output: i am happy Desired output: i ...
0
votes
1answer
27 views

Using a bash loop to run a program that uses single-quotes in the command line, where single-quotes nullify the bash script's intention

The problem: I want to be able to run a program within a bash loop over each .fna file in a directory, but I also want the name of the output file to have the same file name (without the extension), ...
2
votes
2answers
223 views

Getting access to a variable inside a sudo clause in a script with EOF

PROBLEM: I need to have a variable here called $user: chown $user:$user "$HOME"/.bashrc but it doesn't have access to it from outside the sudo and EOF: for user in "$@" do if [ "$user" = root ]...
0
votes
1answer
28 views

Passing python variable to embedded shell script?

I have an existing python code and i need to call a bash script from inside. The python code captures few variables and i need this to be passed to my shell to avoid duplicate input from user. I ...
1
vote
1answer
56 views

Why is Awk messing up my script variables

I really don't know why my variables are getting messed up, but I suspect the cause is awk. I've condensed this problem down to a small script. I'm sorry I can't explain the problem better, but I ...
1
vote
1answer
42 views

Saving find command output to variable [duplicate]

I'm trying to save the find output to a variable in this manner: a=find /Users/Downloads/DTI_allsites/subprojects/*/UII_0077 -name "dti_preprocessed" -type d But I keep getting the following ...
1
vote
3answers
73 views

Bash - How to use a variable in a variable's name

Yes, yes, I know you're probably like "Hey, there are hundreds of other people asking this same question", but that's not true; I'm not trying to do something like this: foo="example1" bar="example2" ...
0
votes
4answers
80 views

Creating n variables in Bash without assigning them one by one?

How do I create n variables in shell scripts without explicitly assigning them? What I mean is something like a loop that creates var1, var2, var3,...,varx, where x is a variable I set earlier, ...
10
votes
2answers
483 views

Initializing Bash variables - Is it required, recommended or define as you go

Is there any advantage/disadvantage of initializing the value of a bash variable in the script, either before the main code, or local variables in a function before assigning the actual value to it? ...
0
votes
3answers
63 views

Why is a variable global when set with `read variable` inside a `while` loop, but local when set with `while read variable`?

unset myVariable i; while [ -z "$i" ]; do read myVariable; echo "myVariable : '$myVariable'"; i=foo; done; echo "myVariable : '$myVariable'" (the unset is there to allow replaying the ...
7
votes
1answer
213 views

My variable's value is 0, but I didn't set it to 0

Today I noticed one of my zsh functions is not functioning; I investigated the issue, and the culprit was this: for i in a b do echo "$i" done 0 0 Then I opened a new zsh, and in that, things were ...
2
votes
2answers
70 views

Using sed with an integer variable [duplicate]

I want to sed using a variable. For example, sed -n '1,10p' file. So, instead of 10, I want to use $k where k is an integer.
3
votes
2answers
99 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) ...
0
votes
2answers
46 views

How to assign .* as a value to variable in bash [duplicate]

When I am assigning .* to a variable , it is assigning all hidden files. [root@s1 ~]# a=".*" [root@s1 ~]# echo $a . .. .bash_history .bash_logout .bash_profile .bashrc .cache .config .cshrc .history ....
1
vote
2answers
55 views

Add Two Spaces in a variable

I need to add two consecutive spaces to a variable. for e.g I want to write Feb 5 (with two spaces in-between) in a variable. I am using the following command but I get Feb 5 instead. I am using AIX....
1
vote
1answer
35 views

how to use variable with awk [duplicate]

we have the following file cat /tmp/hive.conf "hive-exec-log4j2" : { "tag" : "TOPOLOGY_RESOLVED", "version" : 2 }, "hive-interactive-env" : { "tag" : "...
3
votes
1answer
34 views

Awk output to read in a section

I'm trying to output a variable & input to a new command: jira.sh --action createIssue --project "BLAH" --type "Incident" --summary "THIS IS A TEST" --components "BLA" --priority "BLAH"| awk '{...
8
votes
2answers
816 views

Using shell variables for command options

In a Bash script, I'm trying to store the options I'm using for rsync in a separate variable. This works fine for simple options (like --recursive), but I'm running into problems with --exclude='.*': ...
0
votes
2answers
28 views

Directory tree listing, script does not work in csh?

ls -R | grep ":$" | sed -e 's/:$//' -e 's/[^-][^\/]*\//--/g' -e 's/^/ /' -e 's/-/|/' I found a command to display directory tree (see above), it didn't work correctly. It gave error message "...
1
vote
1answer
150 views

How to use multiple variable for input with read command?

I am using GNU bash - version 4.2.10(1). I want to read multiple variables using single read command in shell script. So i tried as below: echo " Enter P N R : " read P N R but it's not working. It ...
1
vote
1answer
36 views

What options are available to copy date output to a variable? [closed]

Context: router outfitted with Merlin AsusWRT. The goal is to append the epoch time to a text file from the UNIX command-line within router. Example: {date:1532348491} The date command return ...
0
votes
0answers
14 views

Command substitution, assignment, tests and spaces [duplicate]

I know the whole language is confusing, but I am having trouble understanding assignments with command substitutions: x=Example sentence #1 fails due to space x='Example sentence' ...
2
votes
2answers
37 views

Number variables appear in directory when I run bash script

I have written a Bash script in a directory. When I run it and then run 'ls' a number appears in the directory level which I must then use 'rm' to get rid of. This Bash script is meant to save the ...
4
votes
0answers
71 views

Why do I need curly braces when using a variable in some versions of sudo -i? [duplicate]

Consider the following command: sudo -i bash -c 'A=2; echo "|${A}|"; echo "|$A|";' When I run this on Debian Linux with bash 4.2.37 and sudo 1.8.5p2 it prints: |2| |2| But when I run it on MacOS ...
1
vote
2answers
64 views

Expanding a variable containing an equals sign and curly braces

I'm writing a script that uses rsync and excludes files based on certain settings. I end up with an exclude flag that has a format of --exclude={foo, bar, baz}. However, upon trying to expand it ...
1
vote
1answer
36 views

if else statement: check if a path stored in a variable exists [closed]

I have problems with an if statement in my script. I have stored a path in a variable and want to use the if...else statement to check if the file (file1) in the path of the variable exists or not. ...
1
vote
2answers
61 views

about dollar sign: dollar sign within dollar sign?

Beginner here. 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 ...
13
votes
4answers
916 views

Is it possible to print the content of the content of a variable with shell script? (indirect referencing)

Let's suppose I've declared the following variables: $ var='$test' $ test="my string" If I print their contents I see the following: $ echo $var $test $ echo $test my string I'd like to find a ...
2
votes
2answers
104 views

Shell Command Substitution Difference/Anomaly

When scripting in Bash, we learned to assign a variable to the output of a command, we can use the construct: variable=$(command-string) This is known as command substitution and executes the ...
2
votes
4answers
62 views

To be able to use a variable with $ in its value as current user as well as some other user through the su command

Suppose a variable value has $ symbol (storing path of a java inner class). I want to process it as current user as well as some other user (assume current user to be root so that I need not enter ...
0
votes
4answers
303 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="...
0
votes
0answers
30 views

Sed is not replacing with the value of a variable [duplicate]

I'm using sed to modify a file. In simple terms, my modifier looks as so sed -e '11s/$/ $LinuxUsername/' This is suppose to add in their linux username at the end of line 11, however, it is instead ...
-1
votes
2answers
240 views

map array of arguments and then convert to string

Say I have these bash functions in a script: foo(){ my_args_array=("$@") export my_args="${my_args_array[@]}" bar $my_args } bar(){ echo "number of args: $#"; } foo a b 'c d e' if I run ...
4
votes
4answers
417 views

Pass arguments for use in a grandchild process

I have a list of arguments and three processes: bash_script -> child -> grandchild The list of arguments is aimed at the grandchild. I can modify all three processes. The grandfather script ...
1
vote
0answers
60 views

Bash variable value partially substituted in Curl input

Below is my shell script in which I'm trying to use the value of a variable as input to curl. However, the script is giving an error and when trying to debug the script, I can see that the value of ...
0
votes
0answers
15 views

Directory Creation [duplicate]

As I stumbled across the internet, I ran into a script that had a line that I am not too sure how to figure out. Here is a link to the github page: https://github.com/eivind88/prm/blob/master/prm.sh ...
0
votes
3answers
173 views

Map string from var1 to var2 (lookup)

I want to do case insensitive lookup on VAR2 based on the content of VAR1 that holds some parameters in the form key:value. If a VAR1 key is present replace the value in VAR2. For example for: VAR1=...
1
vote
2answers
111 views

bash: [: missing `]' when the test stored in variable [duplicate]

Why this works: [ -r /tmp ] && echo "tt" >/tmp/taa cat taa tt But the following doesn't and how to fix that, preserving variable for reusability? COMD='[ -r /tmp ] && echo "tt"' $...
1
vote
2answers
45 views

Shell Script : Variables correct format

I do not know how to write variables with sed So, I want to do the following task using for loop: sed -n '1,200p' big_file.txt > 1to200.txt sed -n '201,400p' big_file.txt > 201to400.txt sed -n '...
5
votes
3answers
2k views

How do I capture a MySQL result set in a bash array?

I'm using bash shell on CentOS 7. I want to run a MySQL query from a shell script and iterate over each row of results. If there were 4 rows returned, I thought I could capture the four rows in an ...
6
votes
2answers
507 views

ls with a wildcard

This works from the terminal: ls /dev/sda* I want it in a bash script, using a variable. I tried: device="a" ls "/dev/sd"$device"*" But I get the error: ls: cannot access '/dev/sda*': No such file ...
1
vote
1answer
39 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 ...