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)

0
votes
1answer
16 views

Why can't I use “$whoam” 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. [...
0
votes
0answers
7 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
56 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
119 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
31 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 ...
0
votes
0answers
19 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
35 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
1answer
33 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
40 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
19 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
53 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....
1
vote
1answer
26 views

Systemd's Environment: check variable existence

Does systemd allow this Environment in a service file? Environment=abc=${abc:-'notABC?'} meaning I'd to set abc as abc if abc exist, if not set abc to notABC? Can systemd do this?
0
votes
1answer
19 views

Can lftp get take in a variable name?

I want to have a script that uses lftp to download a zip file with a variable name. Currently I have the following: open server user user pass set ssl:verify-certificate no ls -ltA1 | grep .zip$ | ...
2
votes
3answers
52 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 ...
1
vote
2answers
40 views

Ansible Playbook: Get value from variable

I'm writing my first ansible playbook right now. That's what I have so far. --- - hosts: kolumbus become: yes tasks: - name: Copy RPM copy: src: "{{ item }}" dest: /tmp ...
0
votes
5answers
77 views

echo line with var that contains few lines

i have a file called 'file1.txt' that contain for exemple: bibi.toto jaja.bubu vrtegbvtr.rvgtbtdtbvtd And i want to echo some text with the var content(that contain the file1.txt lines) like that: ...
-1
votes
4answers
143 views

How to remove leading and trailing spaces from a shell variable [closed]

I have variable named tablename and it has leading and trailing spaces (for example, tablename= yyy ). I want to remove the leading and trailing spaces (OUTPUT : tablename=yyy ) and write this ...
10
votes
2answers
2k views

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

I have a function in my .bashrc file and I can't wrap my head around it. 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 &...
1
vote
1answer
48 views

The $@ variable is not working in a for loop, trying to iterate through a users list

PROBLEM: I can't get the $@ variable to do what I want in a for loop, the loop only sends one name into the file while looping, it should loop through all the arguments and write them to the file ...
0
votes
0answers
30 views

Passing values (as shell variables) to 'mysql'?

I have inherited a shell script which passes a few SQL commands to 'mysql' (actually, MariaDB) via a few lines of "heredoc" text. The SQL commands are to: create a new database; create a new database ...
0
votes
1answer
32 views

Replace values in a variable and retain new lines in BASH

This BASH script works as expected. I have a variable ($foo) that contains lines of data, I tell bash to separate at new lines, and I loop through the variable. #!/bin/bash foo=" Original Line1 ...
1
vote
4answers
73 views

How to redirect stderr in a variable but keep stdout in the console [duplicate]

My goal is to call a command, get stderr in a variable, but keep stdout (and only stdout) on the screen. Yep, that's the opposite of what most people do :) For the moment, the best I have is : #!/...
1
vote
1answer
40 views

Filtering Dataset with AWK based on list

I want to get rows filtered from dataframe based on the condition if they are present in the list. So far I have tried this thing.But I am getting unexpected result. The data is getting replicated and ...
3
votes
1answer
46 views

How to test if IFS is unset in ksh93?

It seems easy to test if a variable is unset. I know of three ways at least: #!/bin/ksh unset var [ "$#" -gt 0 ] && var=$1 [ "${var+set}" != "${var}" ] && echo set1 || ...
0
votes
0answers
19 views

Variable an a loop behaviour in bash script [duplicate]

I encounter a problem of comprehension about variables usage into a bash shell script. To illustrate my case I made a simple script: #!/bin/bash declare -i myVar=12 cd /home find . -maxdepth 1 -...
0
votes
1answer
68 views

Append lines from csv file to another based on column 1 matches

I have 2 .csv files, file1.csv and file2.csv. Say a line from file2.csv has the same 1st field as a line from file1.csv. I would like the full line from file1.csv to replace the full line from file2....
1
vote
2answers
32 views

io redirection not working when command stored in a variable

The following command lets me check if a webpage has specific content. It does not output anything unless the content is found. curl -v --silent https://somedomain.com/somepage.html 2>&1 | ...
0
votes
2answers
44 views

sed with variables containing /

i have a text file containing some content like that, some content replace content other content and i have some variables, looking like this: PATH="/path/to/scriptdir" PARAMETERS="para1 ...
0
votes
0answers
35 views

Scripted “find & rsync” without parent folder

I have written a small "find & rsync"-script to copy photos and videos from the last month & different folders, to a new folder, with three sub-folders. The script looks like this: #!/bin/...
0
votes
2answers
29 views

How to get the filename part of a path from a shell variable

A shell variable holds a path. How does one get its filename portion? In bash(1), I experimented and found that I can do it with ."${i/*\///}" where i is the name of the environment variable. Such a ...
1
vote
1answer
325 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
14 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
112 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
66 views

Default variables in Bash

The default variable for loops in Perl is $_. Is there any equivalent of this in Bash?
0
votes
4answers
51 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
345 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
41 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 /...
1
vote
2answers
62 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
61 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
31 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
514 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
217 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
58 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
109 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
76 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
82 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
576 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
114 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
228 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
74 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.