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 vote
0 answers
34 views

How to run input (x|grep y) in command

lets say when I execute command and then grep it for a specific output. for example: man cat|grep "Written" This gives the name of the authors. My problem is I want to do this via read. here ...
user avatar
3 votes
1 answer
99 views

How can i use a parameter as an inner array-name to my script?

When i write a script named array_call_self.sh as follows #!/bin/bash declare -A num word word=( [a]='index_a' [b]='index_b' [c]='index_c' ) num=( [a]=1 [b]=2 [c]=3 ) array=${$1[@]} for i in ${$...
user avatar
0 votes
1 answer
38 views

How can I get the size of a variable, string, or array in bytes?

For example, if I build an array: $ array1=($(find /etc -mindepth 1 -maxdepth 1 -type d)) $ echo ${#array[@]} 105 $ for each in ${array1[@]}; do echo $each ; done /etc/alternatives /etc/apache2 /etc/...
user avatar
0 votes
0 answers
40 views

Weird behaviour of string concatenation in Bash [duplicate]

I have a problem with bash and string concatenation. Let's say that I have 2 variables: FILENAME=/rust-analyzer-x86_64-unknown-linux-gnu.gz URL=$(http --headers https://github.com/rust-lang/rust-...
user avatar
  • 111
0 votes
3 answers
47 views

Curl against list domains from a file not working

I have a bash script below that is intended to curl against a list of domains, from a file, and output the results. #!/bin/bash baseurl=https://csp.infoblox.com domains=/home/user/Documents/...
user avatar
  • 587
0 votes
1 answer
34 views

Variable Scope in ZSH: How to access global variables from within function?

I have an ever-expanding .zshrc file that was getting out of hand so I decided to try to fix it by breaking it into modular files and sourcing each of them. This works for the most part, but I seem ...
user avatar
  • 113
-2 votes
2 answers
41 views

Why does echo in Bash modify the content of a independent variable?

I have the following code (minimal example). #!/bin/bash ...
user avatar
  • 192
1 vote
3 answers
77 views

How do you expand a variable in a heredoc as one argument when its value has spaces?

I've created a script to upload via lftp: #! /usr/bin/bash set -xe if [ -n "$1" ]; then # ... else source="." # target=name of current local folder target="${PWD##*/}&...
user avatar
2 votes
1 answer
53 views

bash associative array where values are variables, print each value's variable name

I have the following associative array. var1="dog" var2="cat" var3="moose" declare -A asar01=( ["one"]="$var1" ["two"]="$var2" [&...
user avatar
  • 285
0 votes
1 answer
28 views

GNU parallel export function output to variables failed

This script is to determine when destination file already exist, source file will update the destination one or be removed according to flag "$dup_act". #!/bin/bash dup_chk() { # $1: f_src,...
user avatar
1 vote
2 answers
54 views

Why doesn't watch bash -c '"echo $RANDOM"' print anything?

I expect this command to print a random value to the screen every 2 seconds, but instead it doesn't print anything: watch bash -c '"echo $RANDOM"' This command, however, does print a random ...
user avatar
  • 159
1 vote
1 answer
37 views

Assign variable to a file downloaded via curl with origin filename preserved

I have looked through several examples but I cannot find what I need. I am trying to dynamically assign the name of a file I am downloading to a variable so that I can refer to it later. I have to ...
user avatar
2 votes
1 answer
33 views

count variable not getting incremented

I have a scenario where I am running the following code: error_count=0 (cd tmp1 && terraform init -backend=false > /dev/null && terraform validate && echo "Terraform ...
user avatar
  • 85
1 vote
2 answers
45 views

Trying to get path into variable; permission denied

I have 20 files of interest in a directory. They can be split into 5 groups, each group identified by a number from 626 to 630 in the file name. For example, there might (hypothetically) be a file ...
user avatar
  • 177
1 vote
2 answers
48 views

Ansible: why task is skipped when: var is true

From below examples Why DEBUG 2 has been skipped when db.x86_64.alpine.update is true I have been also trying "when: db.x86_64.alpine.update|bool is true" but this also fails Variable file ...
user avatar
0 votes
2 answers
16 views

How to extract the first backup id from a shell output to a variable?

[user@laptop ~]$ cf adbr list-backups database Getting backups of database Backup ID Time of Backup bbf4277f-889a-4a9a-9274-683ed6e1553c_1647592205 Fri Mar 18 ...
user avatar
  • 3
0 votes
2 answers
46 views

How to move all subfolders containing a string to new directory, including its superdirectory

I've been stuck trying to figure this one out. Here is an example of my files: ~/download/Video/CE/ ~/download/Video/CE/153 ~/download/Video/CE/153/2022-03-04 ~/download/Video/CE/153/2022-03-05 ~/...
user avatar
0 votes
0 answers
6 views

how to run K8s pod replicas with few variable values in their conf?

I have to run some containers in K8s, more than 1 replicas for a microservice. I'm using configmaps to load the configuration of the microservice. There are few values that need. to be incrementally ...
user avatar
  • 1,591
-2 votes
1 answer
40 views

Reading values from variables

I am trying to read values from variables like this server=$(cat "$CONF_PATH" | grep "DevServer" | cut -d'=' -f2) echo "server is : $server " port=$(cat "$CONF_PATH&...
user avatar
  • 371
0 votes
2 answers
81 views

Save results from awk to a variable

I have a variable (that contains multiple lines of text) that I'm modifying using awk to replace the whitespace between fields with a comma and I'm trying to save it to another variable. This is what ...
user avatar
  • 3
0 votes
0 answers
29 views

Disable Bash completion for a single environmental variable

Suppose I have an environmental variable $SAVE_DIR=/etc/ used to return to a saved directory (here /etc/) from any Bash shell. Typing cd $SAV[Tab] expands to cd $SAVE_DIR which is quite convenient. ...
user avatar
  • 101
-2 votes
1 answer
65 views

I believe this is a quoting and a variable scoping question

The following is a recommended solution. I ran it and it works. #!/bin/bash function exit_handler() { local device_name="${1}" echo "Exit handler: ${device_name}" } function ...
user avatar
0 votes
0 answers
41 views

Bash script ignore SLURM variables [closed]

I wrote a bash script to automate qm calculations. For this i need to write an executable file .sh which can be submitted to SLURM. Unfortunately, the Script replaces the SLURM environmental variables ...
user avatar
1 vote
1 answer
26 views

Invoke a program in a Bash script with command line parameters stored in a variable

Is it possible to invoke some program in a Bash script with complete command line parameters (both the key and the value) stored in variables? I use the following scanimage call in a script: scanimage ...
user avatar
0 votes
1 answer
26 views

Assign default var if no argument, otherwise treat var like its not there?

I've got a sync script that runs a few rsync and rclone commands. I'd like to add an easy way to run the script in dry mode by default by passing the '-n' switch. I'm running into an issue with this ...
user avatar
  • 3
0 votes
1 answer
42 views

Why echo is printing string variables before additional text that have been merged with it?

I have code in bash for making latex file and in this fragment I want to make a table in a loop: while IFS= read -r line; do if [[ "$line" == *"comment"* ]]; then ...
user avatar
0 votes
0 answers
42 views

Share variables in xargs

I have a function, say, xfunc, with something like this xfunc() { # Infinite loop while :; do # Do some actions echo Hello # Counter?? done } The question is how ...
user avatar
1 vote
1 answer
53 views

Here-document with two variables

Trying two variables with a here-document to generate a list of bash scripts. I ran into the problem that I cannot put the two variables into the script properly and the output file (only one file) ...
user avatar
  • 61
1 vote
1 answer
149 views

Bash script to read all files in a directory, adding their filenames into a string for use in a command line argument

I have a bash script that does something like this: java -jar executablefile.jar --width 100 --speed 50 --ouput outfile.mp4 --input file1.dat file2.dat file3.dat (etc) I am able to read all the files ...
user avatar
  • 111
1 vote
1 answer
168 views

Is there a variable-value clone function in Bash and if not, why not?

In PHP, one can copy the variable-value of one variable to another via, for example: $copy = clone $original; This is not the place to ask questions about PHP but I misunderstand why adding the word ...
user avatar
2 votes
1 answer
47 views

Output to variable or file using (find with -exec and shred)

I have a find command that I exec to shred and it works great; however, I need to capture the output of this and pass to a variable or a file. I have tried so many iterations of the below commands, ...
user avatar
0 votes
1 answer
42 views

Assigned variable has empty output from nslookup and ping commands

I am trying to assign an resolved IP address to variable: ip=$(ping -q -c1 -W1 google.com | grep -Eo "([0-9]+\.?){4}" | head -n 1) | echo $ip or ip=`resolveip google.com | head -n 1` | echo ...
user avatar
  • 3
0 votes
0 answers
19 views

Something in the file is messing with ZSH [duplicate]

I have a file with such lines: map_Kn Ellie_Caruncle_N.png I'm trying to extract field #2, and use it twice in a command like this, but it's giving me some problems. Here is a ZSH session: ~/Downloads/...
user avatar
  • 1,469
-1 votes
1 answer
29 views

How to store a command output as a variable while erroring out if it fails?

I am trying to do an if statement based on a variable that is command, for instance: ssh_location=$(which ssh) My problem is that, I want to store this only if it exists on the system so I can do ...
user avatar
0 votes
1 answer
26 views

Creating a directory from many variables

I want to create a script that creates a new folder with the name of the first argument in the directory specified as the second argument. #!/bin/bash -e ## Passing Arguments (fastq data and ...
user avatar
3 votes
1 answer
116 views

Automatically set an environment variable to be equal to another variable

I would like to create an environment variable which is an "alias" of another environment variable. i.e. whenever the original variable changes, this alias variable changes with it. How do I ...
user avatar
  • 31
0 votes
1 answer
50 views

test -R True if the shell variable VAR is set and is a name reference

First I follow this answer then I search about test -v then https://linuxcommand.org/lc3_man_pages/testh.html shows that there is an R option. test -R seems related to name preference. Then I search ...
user avatar
  • 411
0 votes
0 answers
49 views

how to expand a variable with backslash in bash script [duplicate]

i read a line from another file that contains a backslash, and i want to echo this line without expanding the backslash : script.sh : #!/bin/bash while read line do echo "$line" done &...
user avatar
  • 111
0 votes
1 answer
49 views

Broken variable assignment with elif statements in Bash

#!/bin/bash if [[ $(which yum) ]]; then OS="CentOS" elif [[ $(which apt) ]]; then OS="Debian" elif [[ $(which apk) ]]; then OS="Alpine" elif [[ $(which zypper) ]]...
user avatar
1 vote
2 answers
170 views

Require curly braces in bash variable expansion

Is there a bash option or another way to make bash to only expand curly-braced variables like ${var} and ignore regular ones like $var? Update - here is why i want that: I have a relatively large ...
user avatar
-1 votes
1 answer
145 views

Define multiple variables inside while loop

I'm currently reading from a csv file and iterating through each line to define variables in some columns that I will use later. Say my csv file looks as follows: Column1,Column2,Column3,Column4,...
user avatar
  • 31
1 vote
2 answers
51 views

Iterate lines on csv to save columns as variables

I have a csv composed as follows: Column1,Column2,Column3 A Existing text in Column1, A Date in Column2, A Integer in Column3 B Existing text in Column1, B Date in Column2, B Integer in Column3 C ...
user avatar
  • 31
0 votes
1 answer
53 views

Is there a way to export functions with parameters already expanded?

Let's say I have two files main.sh and sub.sh in the same folder with the following contents: main.sh: #!/usr/bin/env bash export PARAMETER="main" my_func(){ echo "$PARAMETER $1"...
user avatar
  • 3
1 vote
0 answers
11 views

Referencing a time-style definition does not work [duplicate]

For a given file one may issue the bash command: ls -la --full-time --time-style long-iso --time-style=+"%Y-%m-%d %H:%M:%S" file This works of course. I have a larger script in which I ...
user avatar
  • 21
1 vote
1 answer
168 views

Ansible: group_names variable expansion doesn't work

In ansible i created a sshd_config which will be copied on servers which are part of sit group [sit] 192.168.18.10 192.168.18.11 192.168.18.12 192.168.18.13 192.168.18.14 192.168.18.15 - name: Deploy ...
user avatar
  • 3,834
2 votes
0 answers
64 views

Why `declare -p VAR` returns "declare -- VAR" on an unset local variable but gives an error "bash: declare: A: not found" on an unset global variable?

I don't understand: $ declare -i VAR=0; \ > echo "$A"; \ > fun() { local -i VAR=1; echo {; echo "$VAR"; declare -p VAR; unset VAR; echo "$VAR"; declare -p VAR; echo ...
user avatar
  • 234
0 votes
2 answers
73 views

For-loop - appending to arrays with iterator in the array name

I have the following problem. I have an array arr with some values. I want to sort each value into a set of different - and already declared - arrays earr$j, i.e. arr[0] into earr1, arr[1] into earr2 ...
user avatar
2 votes
0 answers
65 views

for loop and appending over list of arrays

I am new to Bash scripting and presently find myself dealing with a small problem in working with for loops, arrays and variable assignment/substitution which I do not know how to solve. Since I am ...
user avatar
0 votes
1 answer
50 views

Why doesn't `eval` work in this code?

I wrote the following code which is meant to create a file with a list of low-resolution media files: #!/usr/bin/bash find "$PWD" -type f -iname "*.avi" -execdir ~/CS/...
user avatar
2 votes
2 answers
67 views

Mounting devices to mountpoints stored in variables

Im doing this script and I cant see to find a way to accordingly mount the devices stored in the "devs" variable into the mount points listed in "mntpnt". Hope Im being clear ...
user avatar

1
2 3 4 5
23