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)

Filter by
Sorted by
Tagged with
-1
votes
0answers
12 views

Why variable substitution is also called variable reference?

Why is variable substitution also called variable reference? In C/C++ the reference operator (&MyVariableName) returns the pointer address of a variable. In bash programming instead, we talk about ...
0
votes
1answer
16 views

Filter directory tree and create user select menu from results

I have a Google Drive folder in the cloud that I sync with my server to backup and restore a WordPress web directory and MySQL database dump and I accomplish this with rclone. I am able to see the ...
0
votes
1answer
35 views

How to put piped input into a variable

I'm new to bash so this should be pretty basic but I can't find it so far. I am going to have an input cat input_file | sh_file.sh output_file I want to get the information from the input_file so ...
0
votes
1answer
28 views

Script using data/input which contains variable names [duplicate]

So I have a file "Directories.dat" that contains a list of directories for a script to read that runs on multiple machines. Due to this, the list of directories in the file is often defined ...
0
votes
1answer
37 views

zsh - How do I make local variables available to inner functions that are not defined with 'eval'?

I have a zsh function that looks like this: outer_func () { local LOCAL_VAR='test' inner_func () { echo "${LOCAL_VAR}" } } where $LOCAL_VAR is not available to ...
1
vote
1answer
37 views

A variable inside an another variable

I have these variables a1=0.017 a2=0.2 a3=10.7 a4=20.9 a5=35.4 for ((x=1; x<=5; x++)) do for i in a${x} do echo "Welcome $i times" done done The output must be: "Welcome 0.017 ...
0
votes
1answer
29 views

find cannot do on having space within string variable for -path or -iregex or etc

Cannot have space within string variable for -path or -iregex find's option. This input '/home/demo.*/\.config/File System/.*t$' is fed on: read i m="-iregex $i" find ~ $m won't work as ...
2
votes
0answers
48 views

What are the allowed values for a Bash variable?

I thought that a a Bash variable (of size 1) can hold any byte from 0 to 255. So any bitstream (in particular files) can fit in a Bash variable. But according to a comment I received, it cannot hold ...
0
votes
0answers
27 views

How to concatenate values of one variable and other string without missing information? Is the a bug at ubuntu wsl?

What is my linux? I use ubuntu on WINDOWS 10 PRO - windows subsystem for linux. Context the problem: I have two files. The first file is the name 'paths-linux.sh'. Your content is: CAMINHO_UM="/...
0
votes
1answer
40 views

Bash script fails with spaces in absolute path variables

I am running following script to copy files from CIFS share mounted on the system to another destination system. The absolute path of CIFS share contains few spaces and so it fails for that path, I ...
0
votes
1answer
76 views

How to output comma separated values using IFS=“,” in shell [duplicate]

I have a small script, which does not give comma separated output when IFS is used, but IFS is mandatory as i need it to read many other value. The output of the below script is odi_server1 ...
1
vote
1answer
38 views

Using variables for Ghostscript options/flags causes errors

I am scripting the creation/manipulation of PDF files so I am using gs (Ghostscript). The problem I am having is that if I use a variable for the the options, gs errors out. The command that I am ...
0
votes
1answer
27 views

Feed script variable to command that prompts for user input

I have a shellscript ABCD.sh and it will have a command to upload a file onto server which will prompt for username and password. Instead of providing username and password there, I would like to pass ...
-1
votes
1answer
26 views

variable regex test in dash?

I got plenty possible variables in a form of [a-z][0-9], eg 1 letter and 1 number. Is there a way to test them in dash? Something like: myvar=o7 if [ $myvar=o[0-4] ]; then "do this" elif [ $...
0
votes
3answers
20 views

How to force Dialog to write checklist selections onto sperate lines in a variable

I am using the below code to present users with a menu for them to select which software they would like to install. I then want to use the selections as function calls to perform the installs. ...
0
votes
3answers
34 views

Not able to get the values in JSON from Variable

I have written small Shell/bash script, where I have some data in sampleData.txt file which I want to convert in Base64 and to pass it in variable $scriptPayload: value=$(cat sampleData.txt) echo &...
0
votes
0answers
15 views

in-command variable initialization [duplicate]

Why DISPLAY=0:0 startx work but MYVARIABLE="yes" echo $MYVARIABLE doesn't?
0
votes
1answer
36 views

Global shell varible resets on shell startup

I've tried using fzf, and I've done export FZF_DEFAULT_COMMAND as per the instructions. I set it to ag -g "". For the rest of the session, echo $FZF_DEFAULT_COMMAND gives me ag -g "&...
1
vote
2answers
83 views

Creating nicknames for command arguments

I would like to use a nickname (foo) for my email address (foo@domain.com). The goal is to be able to do echo "This is a test email" | mail -s "Email Title" foo instead of echo &...
4
votes
1answer
82 views

How to ensure user input consists of exactly 6 digits

I am running HP-UX 11.23 and have a bash script that reads two inputs. Both inputs need to be exactly 6 digits long. What is the best way to read the input and if its not 6 numbers ask the users to ...
1
vote
1answer
55 views

gnuplot: Can you set shell variables in a script and then later call them?

I want to be able to issue shell commands from a gnuplot script, such as setting a variable, but it appears that the system command spawns a new shell instance as opposed to issuing commands to the ...
0
votes
1answer
49 views

Whats the most efficient way to update a variable in a file?

Short Question: Whats the most efficient way to update a single numbered variable in a file? Long Question: I'm looking at the output of a security program (fail2ban) which logs which hosts have been ...
1
vote
0answers
49 views

Two lines are removed with sed instead of one, why? [duplicate]

I am attempting to remove lines from this text file (foo.txt): cat mouse animals: 2 I want to remove the two lines from the end: ‎ animals: 2 So I end up with this: cat mouse However, when using ...
1
vote
1answer
30 views

Ansible - Access group variables in tasks

I have my inventory file configured with group variables. Example: all: children: europe: vars: network_id: 3 network_name: "europe-eu" hosts: ...
1
vote
1answer
57 views

How can i put a variable in an awk command

with my code im trying to sum up the values with the specific name of a column in a csv file, depending on the input of the name. Here's my code: #!/bin/bash updatedata() { index=0 while IFS=...
0
votes
0answers
52 views

Run multiple Ansible playbooks with Python generated var as Argument in one playbook

I have python script which generates variables var1 and var2 These variables are used as extra var arguments for Ansible playbooks. The script variables generated dynamically by python scirpt and ...
1
vote
1answer
67 views

Pipe partial output of background process and store it in variable

Background: I'm working on a project in which a server is started at a random available port. The server is started by a command: node server.js Once the server is ready and starts serving on an ...
0
votes
2answers
36 views

one line definition of a variable conditional on the output of a logical test

In bash programming I've always defined conditional variables in the following long hand way dog=1 if [[ $dog -eq 1 ]] ; then cow=1 else cow=0 fi This is obviously incredibly inefficient. In ...
0
votes
1answer
59 views

bash adds single quotes to > (redirect) during variable expansion (breaking my redirect) [duplicate]

After research, Im not certain this is possible in a bash script. The most likely reason ive found is due to bash's order of shell expansions. (maybe?) What i would like to happen, Is the ability to ...
0
votes
1answer
35 views

must PS1 string be reevaluated each time input enter

How to make the PS1 string always be reevaluated each time the user inputs the enter key at the shell prompt? actual in mine: n=A; PS1="$n" n=B The prompt keeps showing A. How to solve it?
0
votes
0answers
11 views

Can't filter file extensions when the list is in a variable [duplicate]

I guess this is a relative simple question, but I can't find the way to access the variable content when doing a git diff, so it lists all the files with specific extension. // This works fine git ...
1
vote
1answer
30 views

extract 5 first lines of a text file to a variable

I'm wondering if it's possible to "extract" the first 5 lines of a textfile to a single variable (not an array) for example: head -5 test.txt >$variable (which of course doesn't work) I'm ...
1
vote
4answers
59 views

String substitution to detect empty variables?

I have a script that iterates through files and does some string substitution to insert the date. #!/bin/bash f="/tmp/file.txt" # with .txt extension timestamp="$(date +%H%M%S)" ...
0
votes
1answer
60 views

Confused about the naming convention of variables in shell scripting [closed]

I am slightly confused if there is any rigid rule regarding the naming of variables in Shell Scripting. I found different answers in different locations: Unix / Linux - Using Shell Variables (on ...
0
votes
1answer
58 views

How to print variable value as a hex number?

I can find seemingly every variation of hex manipulation by printf except this one. I am trying to send html hex colour values to a text file, built mostly using printf. I can calculate the separate R,...
4
votes
0answers
62 views

Using AWK to filter a file based on the numerical value of a column (where the column number and numerical value are passed in from the command line) [closed]

I have a file that is tab-delimited. I am trying to use AWK to filter the file such that the user can specify a column number and a number, and the script will return only the lines of the file (and ...
2
votes
3answers
157 views

How do I set a value when running find command?

I'm running a long string of -exec commands, and I want to pull a timestamp from the beginning of the run to use as a value for subsequent commands. Is this possible? Something like: find . -exec NOW=$...
1
vote
3answers
50 views

Add basename to output files

I have a script which processes input files and generates an output file. I want the basename of each file to be part of the output file, but it isn't working: while IFS= read -r line do tmp_header=&...
1
vote
2answers
55 views

Using a variable within a string

I'm stuck on a problem. I am a biologist who uses linux for automation so I don't have the best programming background and would appreciate your help. I have a large text file, containing several ...
0
votes
1answer
41 views

Bash array arent adding elements to last

root@kali-linux:~/Softwares/Softwares/Tools/dirsearch# array=() root@kali-linux:~/Softwares/Softwares/Tools/dirsearch# for i in 1 2 3 4 > do > array+=($i) > done root@kali-linux:~/Softwares/...
1
vote
2answers
395 views

How do I assign a value to a BASH variable iff that variable is null/unassigned/falsey? [duplicate]

I'm looking for the equivalent to this JS assignment: FOO = FOO || "I must have been falsey!"; Thanks!
0
votes
3answers
120 views

glob variable mandatorily in the double quotes

This link: Wildcards inside quotes in my case not solve the problem. The file "a.sh" contain: file="path with wildcard*" The file "b.sh" contain: . a.sh [ -f "$file&...
1
vote
2answers
34 views

Echo and variable expansion

I have a script to generate a script: echo '#!/bin/sh'>>rule-1 echo 'compare() (IFS=" "'>>rule-1 echo ' exec awk "BEGIN{if (!($*)) exit(1)}"'>>rule-1 echo '...
1
vote
2answers
66 views

'export'ing a variable in shell script [duplicate]

We have two scripts, first.sh and second.sh. We run first.sh from second.sh using the . (source) command. I am stuck at this script, first.sh value="myvalue" export value oldvalue="othervalue" ...
1
vote
1answer
31 views

Result of a command is not assigned to a variable

I am pretty unexperienced (to put it mildly) when it comes to BASH and Shell scripting, so bear with me: $ toda=$(date) | echo $toda gives me: Fr 29 Mai 2020 15:25:19 CEST. So far so good. datediff ...
0
votes
0answers
28 views

What are the equivalent data objects in Bash in comparison to R?

R was the first programming language I learned. It is always a pleasure to learn a new language associated with the first language. Here comes my question. What are the equivalent data objects in ...
-1
votes
1answer
22 views

bash command variable creation

I have this command that I've created and would like to place it in a variable. sort -k 1,1 | tr " " "\n" | users | wc -l Such as the one below, however, as of now the output produces an error and ...
1
vote
1answer
35 views

How to do output of both variable and it's value to a file

I'm struggling a bit with this task, for example I can do a simple loop to send variables values into a file with for loop. But how to get something before output of the variable value? I did ...
0
votes
1answer
91 views

using shell variable in awk and using for/while loop

How can I loop through and achieve below with passing shell variable in awk using for/while loop? I have a text file like below. mark 10 20 30 lawrence 40 22 60 mark 11 12 13 mike 15 16 ...
1
vote
1answer
22 views

My multiple variables in a function do not get expressed as aliases. I think my syntax is faulty

In bashrc, I have this alarm function which can take 3 variables: a () { local $1="${1:-3600}" local $2="${2:-paa}" local $3="${3:-alarm}" sleep "$1" && $2 && $3 } alias ...

1
2 3 4 5
20