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
1answer
27 views

conditional binary operator expected

var="$(command1 -l '$var2' -c 'command2|grep -c "search"')" if [[ var !=0 ]]; then fi Why am I getting "conditional binary operator expected". I searched already and. I. see that [[]] is a test ...
1
vote
2answers
53 views

SSH find command not recognizing directory stored in local variable

I'm able to SSH and go to the directory stored in a local variable $out_dir. However, I cannot figure out why the find | head command won't work. In Try #2, I added a second backslash at the end of ...
0
votes
1answer
45 views

bash script - how use a $variable in grep

I am writing a simple bash script where user input the domain name (example.com) and it greps the email address from whois command output. I want only grep the email with the same input domain (xxx@...
0
votes
0answers
43 views

Variable losing value [duplicate]

Anyone have suggestions? Thanks in advance. #!/usr/bin/env bash VAR=$( ssh -q -T -o ConnectTimeout=10 server01 " df -Ph" ; EXITSTATUS="$?" | sed -r '/^\s*$/d' | grep -v "test" | wc -l ) echo $...
1
vote
0answers
28 views

Variable not available outside function [duplicate]

I'm having difficulties understanding the following example I created. The variable glob_var gets changed inside the test() function, but the original value remains outside of the function: test() { ...
0
votes
0answers
45 views

Problem with variable with special characters

I need to set a variable with special characters to run in several hosts with command for VAR="echo | awk -v VARIABLE=`uname -n` '{ print VARIABLE "|" $1,$2,$3,$4,$5,$6,$7,$8,$9}' /etc/passwd" and I ...
1
vote
3answers
110 views

Multiple commands and subshell execution after pipeline

Ok, I know that in Bash (by default, without 'lastpipe' bash option enabled) every variable assigned after a pipeline is, indeed, executed in a subshell and the variable itself dies after subshell ...
0
votes
3answers
437 views

Assign to a variable in a bash script

I have the following script and I try to create an empty text file. I tried touch and I tried echo -n > - they both fail. #!/bin/bash set filename_result="contents" echo "filename=$...
1
vote
1answer
63 views

Copy bash array to a variable which name is hold by another variable

There are many similar questions, but I didn't understand any answer as solution of the following problem: I want to create a bash function which holds two arguments, first is a variable name and ...
0
votes
1answer
44 views

Extracting last ten characters of a string (e.g., file name) inside a parameter expansion [duplicate]

I have a file name (including an extension, but not a full path name) in variable file, and I want to only get the last 10 characters of the base name from a parameter expansion. Example: file ...
0
votes
2answers
29 views

Passing looped variable to ansible role

Is there a way to pass a list of values to /vars/main.yml file to subsequently pass those variables to the tasks for processing. The intention here is to running the task once rather than N times. ...
0
votes
2answers
53 views

How can i store commands as a variables and execute them randomly in bash?

How can i store commands as a variables and execute them randomly in bash? command1=" convert -size 2000x1000 xc:none -gravity center \ -stroke yellow -pointsize 50 -font Courier-BoldOblique -...
1
vote
1answer
28 views

Variable from variable [duplicate]

What am I doing wrong here? A_B_NAME="something" X=A Y=B RESULT=`echo \${X}_\${Y}_NAME` echo ${RESULT} and I'm always getting A_B_NAME as a result, but want "something" Thanks! Michael
0
votes
3answers
47 views

Why test -n returns true even when a variable is empty? What am I doing wrong? [duplicate]

according to the test(1) manpages: -n STRING the length of STRING is nonzero so I expected this to run fine: [ -n ${var} ] && echo "var is not empty" I used this ...
0
votes
1answer
28 views

User input to search bash array

folks-- I'm getting kinda stuck, here. I have a small script that will be incorporated into a larger script. This small piece is supposed to take a user input, and compare it to a list of stored ...
-1
votes
2answers
16 views

conditionally assign var with check for defined and condtion [duplicate]

I want to assign a variable conditionally. This does not work [[ -v $2]] && [$2 == "init" ] && command="./ops/init.sh" [[ -v $2]] && [$2 == "destroy" ] && command="./...
1
vote
1answer
39 views

Makefile - Set multiple variables on a single stage

I have this Makefile where I'm having some troubles on simply set some variables: my_stage: echo "FULL_NAME=$(FULL_NAME)" echo "MY_NAME=$(MY_NAME)" $(eval SOME_NAME=$(shell sh -c "...
2
votes
2answers
38 views

Search for a String in a file and save the string after it to a variable

Example: Lets say this is in a file: AB: Test CD: Testing EF: Rest I want a script to search for CD and save "Testing" into a variable. I've tried something with awk but somehow I'm not making any ...
0
votes
1answer
21 views

Parse variable from system.log string and run command using parsed variable

My current solution works, but I know there is a better way to do this. I run a tail of macOS system.log for keywords currently. I have a separate script for each keyword I'm searching for, then ...
0
votes
4answers
110 views

Shell script to append single quotes and commas

I have a file named input.txt: $cat input.txt This is sample Input file To execute sql statement I need output like below assigned to a variable: X=('This is sample', 'Input file', 'To execute sql ...
14
votes
7answers
3k views

How can I count the number of files in a directory and delete the oldest if the number exceeds 5

I've created a script to run automated backups on my CentOS 7 server. The backups get stored to the /home/backup directory. The script works, but now I would like to incorporate a way to count the ...
0
votes
2answers
50 views

Why is Brace expansion with variables not working as expected? [duplicate]

I would like to set a variable (x=100) in the brackets such as x=100 for i in {0.."$x"..50} ;do echo $i done the desire output for i should be 0 50 100 although I get as an output {0..100.....
0
votes
0answers
23 views

Add a text to a variable is placed in the front of the variable

With a script we load a file /Parameters/Param.par: Content of Param.par export path_prod=/Directory/A/ The contain of the script is: . /Parameters/Param.par echo $path_prod/log_files.txt My ...
0
votes
0answers
28 views

Able to echo variable but unable to substitute value in java shell command

I am trying to read some variable (username and password) from config file and use them with java command:- abc.config user_id=myid user_password=mypassword jar=/home/abc.jar Now I am ...
0
votes
2answers
60 views

Remove unwanted character from a variable

I've seen a few of these questions, and a few answers, but they only tells me how to echo the results to the screen, and often only refers to one character. I need to remove the chars ', and keep ...
-3
votes
1answer
30 views

Concat two vars to call third var

I am sorry if this has been answered before, I tried searching and was unable to locate any corresponding answers: $a="hello" $b="world" $helloworldtest="worked" echo "${a}${b}test" - prints Hello{...
1
vote
1answer
98 views

Why is “${ARRAY[@]}” expanded into multiple words, when it's quoted?

I don't understand why "${ARRAY[@]}" gets expanded to multiple words, when it's quoted ("...")? Take this example: IFS=":" read -ra ARRAY <<< "foo:bar:baz" for e in "${ARRAY[@]}"; do echo $...
0
votes
2answers
97 views

Why do I Get a Blank Output When echoing $variable? [duplicate]

I'm just trying to echo a variable stored in a file. When issuing the echo $variable command I'm getting a black area with no output. I don't understand why. Content of file1: name="Jhon" My ...
0
votes
1answer
43 views

How to pass a multiword string as argument in Linux [duplicate]

I am trying to pass a multiword argument in a function an echo the result in a simple fashion. Currently I am doing this: function myFunction { multiWordString="" for ((i=3; i<=$#; i++...
0
votes
3answers
134 views

Using linux 'find' command's multiple name feature in a variable?

I newly learned how to search multiple file names with find like: find . \( -iname "*.srt" -o -iname "*.mp4" \) But in some cases I had to use so many filters like this and that simple command line ...
0
votes
1answer
118 views

Random unbound variable error within function

I made a function in bash and when I call it, it crashes with an unbound variable error. I don't understand cause the variables that are said to be unbound are declared. Moreover, it seems to be ...
4
votes
2answers
73 views

how can I run a command from an environment variable and have the internal trailing ampersand work?

I have this small script I call prompt-to-run. prompt_acc='' read -p 'run `'"$1"'` ' -i "$1" -e prompt_acc $prompt_acc It lets me create a script that fills in a command for me, but gives me the ...
1
vote
2answers
79 views

How to dynamically set a variable name in shell script?

I am looking for a .sh equivalent of the following (yes, really.) #!/bin/bash exe1='filepath' n=1 var=exe${n} echo ${!var} Where the echo should output filepath. I want to use plain sh. I have ...
1
vote
1answer
31 views

Variable is not expanded in a command with diff

My shell is zsh. I tried these command on bash, which successed. ignore_files=( local \*.local.\* .bundle Gemfile.lock ) ignore_command="" for f in "${ignore_files[@]}"; do ...
1
vote
1answer
75 views

Can you help me understand this bash behavior? Background processes and their attachment to the current process

I stumbled upon this unexpected behavior and I was hoping someone with a better understanding could explain it! I have a function that is called by a script and run as a background process - ...
1
vote
1answer
53 views

How to have separate arguments/variables in read

I want to make a CLI using bash for fun. I want to have a read prompt like CLI/[path]/:> easy read -p "CLI$PWD/:>" but if I want to do a command like cd then I will have to make a whole ...
0
votes
1answer
58 views

How to declare -A MYASSOCIATIVEARRY globally? [duplicate]

I have the same exact problem as described in this SO post ("bash associative array key string with colon is giving error"): https://stackoverflow.com/q/40406187/10639803 The solution is to use ...
1
vote
2answers
223 views

grep giving no output for a directory passed in a variable

I am attempting to write a bash script that searches contents of files in a specified directory tree for the presence of a specified substring. Using grep's recursive function alone is not sufficient,...
0
votes
1answer
78 views

When using variable in sed it replaces entire line, instead of just the matching string

I'm new to bash and I've been trying to understand other posts that's similar, but I'm just not sure if they do the thing I need. When I don't use variables and just put in what I want to search for ...
0
votes
0answers
30 views

Offlineimap variable do not work, however, there are plenty of examples on the internet

I've seen examples on variable usage for expansion in offlineimaprc like this "%(variable)". This is also in official docs: http://www.offlineimap.org/doc/ConfigurationFactorization.html I need to ...
-1
votes
3answers
270 views

while read loop and variables

I am seeking your council .. as i'm hitting a wall .... in a bash script I'd like to read all of the lines of a given file to an array, and run a set of statements to determine if said args are ...
1
vote
1answer
62 views

How to read keyboard input and assign it to a local variable?

I have this very simple script: #!/bin/bash read local _test echo "_test: $_test" This is the output. $ ./jltest.sh sdfsdfs _test: I want the variable _test to be local only. Is this possible?
1
vote
1answer
41 views

Text processing with sed on a variable in bash not giving expected (modified) output?

We are using some outdated encoded script which does not to be allowed to be modified. Therefor I though of a solution to modify the input automatically by using sed. This didn't work and after ...
0
votes
2answers
55 views

How to pass linebreak from a variable in “ANSI-C” way?

If I run this code #!/bin/bash set -x http --json http://example.com \ value=$'hello\r\n\r\nworld' I have in the stdout the two carriage return I have inside value http --json http://example....
0
votes
2answers
153 views

Awk - How to use a variable as part another variables name

I have a list a variables being used in an awk command. They are passed parameters for a script so their value is undetermined but I am trying to use them in a for loop. My question is can I use the ...
1
vote
1answer
55 views

use wildcard after a variable bash script

I've been trying to make a script merge all csv files to a montly file. The script below does not work, it gives the following error: awk: fatal: cannot open file `/ibv_ftp/merged/folder/file201910*....
1
vote
1answer
36 views

How to read a file line by line and put them as a variable?

im very new to linux and shell scripting. May I know how do you read a file line by line and pass each line as a variable? For example, lets say i have a file called abc.txt with these 2 lines as its ...
1
vote
1answer
58 views

ZSH: how to escape the following command (variable assignment)?

_test=`shasum -a 256 my "file here.txt" | awk -F' ' '{print $1}'` This works as expected. But the OCD in me wants to use something like _test="${my-command-here}" But because of the single quotes ...
1
vote
1answer
75 views

Variables behave unexpectedly in while loop

This is a re-post of the same question in Bioinformatics. The given answer didn't work for me, and I thought I'd ask here. My directories are organized as follows: one main directory, in which I have ...
3
votes
1answer
51 views

Is it possible to pass commands as variables to a --dialog script?

Currently I have the below dialog function that I am trying to make into a reusable yes/no prompt where I will be able to call it using YesNoPrompt followed by variables it will be needing. The ...

1
2 3 4 5
19