The variable tag has no usage guidance.
52
votes
3answers
5k views
What NOT to put on an SSD?
I bought an SSD and I am going to set up my desktop system with a completely fresh Linux installation.
SSDs are known to be fast, but they have a disadvantage: The number of writes (per block?) is ...
28
votes
4answers
20k views
Using “${a:-b}” for variable assignment in scripts
I have been looking at a few scripts other people wrote (specifically Red Hat), and a lot of their variables are assigned using the following notation
VARIABLE1="${VARIABLE1:-some_val}"
or some expand ...
13
votes
2answers
908 views
How can variables use “~” for cd'ing?
How can I use a variable - $BASE in my cd.
I tried the following but I get an error
$ cd ~/z/repo_1_ruby_193/
23:23:57 durrantm Castle2012 /home/durrantm/z/repo_1_ruby_193
$ BASE="~/z"
23:24:03 ...
12
votes
4answers
22k views
Pass shell variable as a /pattern/ to awk
Having the following in one of my shell functions:
function _process () {
awk -v l="$line" '
BEGIN {p=0}
/'"$1"'/ {p=1}
END{ if(p) print l >> "outfile.txt" }
'
}
, so when called as ...
10
votes
2answers
57k views
how can I add (subtract, etc.) two numbers with bash?
I can read the numbers and operation in with:
echo "First number please"
read num1
echo "Second mumber please"
read num2
echo "Operation?"
read op
but then all my attempts to add the numbers fail:
...
10
votes
8answers
13k views
How to assign a string value to a variable over multiple lines while indented?
The issue:
I need to assign a variable a value that is decently long.
All the lines of my script must be under a certain number of columns.
So, I am trying to assign it using more than one line.
...
9
votes
3answers
301 views
Define variables with a variable name
What's wrong with this script?
I'm trying to define A1=1, B1=1, C1=1
LIST="A B C"
for x in $LIST
do
"$x"1=1
done
and the result is:
./x.: line 7: A1=1: command not found
./x.: line 7: B1=1: ...
8
votes
1answer
2k views
How to store an image file in bash variable?
After I use the following command,
pngString="$(cat example.png)"
echo -n "$pngString" > tmp.png
I can't open the tmp.png as a PNG file. Maybe some information is lost when I use $pngString ...
7
votes
4answers
7k views
shortest way to replace characters in a variable
There are many ways to replace characters in a variable.
The shortest way I found out is tr so far:
OUTPUT=a\'b\"c\`d_123and_a_lot_more
OUTPUT=$(echo "$OUTPUT"|tr -d "'\`\"")
echo $OUTPUT
Is ...
7
votes
2answers
10k views
Why is my variable local one 'while read' loop, but not in another seemingly similar loop? [duplicate]
Why do I get different values for $x from the snippets below?
#!/bin/bash
x=1
echo fred>junk ; while read var ; do x=55 ; done <junk
echo x=$x
# x=55 .. I'd expect this result
x=1
cat ...
6
votes
6answers
1k views
How do I check if a variable exists in bash?
I need to check a variable's existence in an if statement. Something to the effect of:
if [ -v $somevar ]
then
echo "Variable somevar exists!"
else
echo "Variable somevar does not exist!"
...
6
votes
5answers
813 views
Number of characters in a shell command's output
I am writing a script which needs to calculate the number of characters in a command's output in a single step.
For example, using the command readlink -f /etc/fstab should return 10 because the ...
6
votes
2answers
2k views
What's the most efficient way to grep for two completely separate things and assign the values to separate variables?
CentOS 6.x
I want to take the output from curl, grep for two completely separate strings, and assign their respective values as variables. What is the most efficient way to do this (without writing ...
6
votes
3answers
725 views
Attempted assignment to non-variable?
We can use arithmetic operations inside shell script function:
function mess
{
if (( "$1" > 0 )) ; then
total=$1
else
total=100
fi
tail -$total /var/adm/messages | more
}
I ...
6
votes
5answers
1k views
Serialize shell variable in bash or zsh
Is there any way to serialize a shell variable? Suppose I have a variable $VAR, and I want to be able to save it to a file or whatever, and then read it back later to get the same value back?
Is ...
5
votes
3answers
939 views
Assigning exit code to a shell local variable
#!/bin/bash
function0()
{
local t1=$(exit 1)
echo $t1
}
function0
echo prints empty value. I expected:
1
Why doesn't t1 variable get assigned the exit command's return value - 1?
5
votes
1answer
1k views
ifconfig -a in a shell script
I am writing a shell script to start my network in my virtual machine on boot, since it does not do this right away for some reason with a snapshot of a virtual machine.
Since the eth device starts ...
5
votes
1answer
139 views
What else can break if I remove export from a variable?
My problem is that when I started coding my script I encountered several variable scope issues which resulted in the bad habit of exporting almost all my variables.
Now that my code has got quite ...
4
votes
3answers
87 views
How to use call-by-reference on an argument in a bash function
I am trying to pass a "var name" to a function, have the function transform the value the variable with such "var name" contains and then be able to reference the transformed object by its original ...
4
votes
2answers
1k views
Setting a shell variable in a null coalescing fashion
I'm really fond of "null coalescing", where you can set a variable to the first "non-null" value in a list of things. Many languages support this, for example:
C#:
String myStr = string1 ?? string2 ...
4
votes
1answer
757 views
Bash: Variable assignment doesn't seem to 'stick' [duplicate]
I am having a hard time tracking down the reason my 'boolean' flag variable won't stay false when a test fails inside a while loop.
The script involves a few loops but essentially it is intended to ...
4
votes
4answers
914 views
How do I output a suggestion for a user to accept and use as input?
What can I do to leave the cursor on the same line of the suggestion in a Bash script?
Bash version = 3.2.51
It's purpose is to suggest a string so that the user should only press Enter.
#!/bin/sh
...
4
votes
2answers
426 views
Running commands stored in shell variables
The following works in my shell (zsh):
> FOO='ls'
> $FOO
file1 file2
but the following doesn't:
> FOO='emacs -nw'
> $FOO
zsh: command not found: emacs -nw
even though invoking emacs ...
4
votes
3answers
384 views
Using | pipe character from a $variable makes it treat as just another argument in bash; how to escape it?
I have a bash script like this
export pipedargument="| sort -n"
ls $pipedargument
But it gives the error
ls: |: No such file or directory
ls: sort: No such file or directory
It seems to be ...
4
votes
1answer
158 views
Length of argument inside a function of an sh script
I have a FreeBSD 9.3 RELEASE server and I am trying to write a shell script using sh.
I am trying to get the length of a variable that is passed to a function in a shell script, like so:
#!/bin/sh
...
4
votes
1answer
160 views
Can I refer to a global variable even if a locally scoped one exists with the same name?
If I have a variable x
total=3
and then a function
function A () {
local -i total=0
}
Is there any way I can update the 'global' total variable total from within function A ?
4
votes
3answers
8k views
Pipe assigns variable
For simplicity I would like to do:
echo cart | assign spo;
echo $spo
Output: cart
Does such an assign application exist?
I am aware of all the ways to do this using substitution.
4
votes
2answers
160 views
Shell script error handling while assigning STDOUT to variable
I am trying to implement error handling in my shell script as described in the 2nd answer in Best practice to use $? in bash?
My script looks like this:
#!/bin/bash
try() {
"$@"
code=$?
...
4
votes
1answer
1k views
Variable scope in while-read-loop on Solaris
Could someone please explain to me why my while loop seems to have an internal scope? I've seen multiple explanations online but they all have to do with pipes. My code has none.
The code:
...
4
votes
1answer
60 views
“$@” expansion for user defined variables
I'm trying to get a (bourne shell) variable to expand like "$@" so it produces multiple words with some having preserved spaces. I've tried defining the variable in many different ways but still can't ...
4
votes
2answers
241 views
Replace string in a file with another where both are saved in variables
I want to replace a string found in a file with another string, but both have a special character (in this example it is a . char) e.g 1.0 and 2.0.
So this is the command currently used:
sed -i ...
3
votes
6answers
396 views
How to keep last exit status after test
Is it possible to keep the last command exit status ($?) unaltered after a test?
E.g., I would like to do:
command -p sudo ...
[ $? -ne 1 ] && exit $?
The last exit $? should return the ...
3
votes
3answers
117 views
What is the difference of defining a variable with or without quotation marks? [duplicate]
If I define a variable with the quotation mark:
TEMP="~/Dropbox"
then
ls $TEMP
would not work, instead
echo $TEMP | ls
works.
And to get the same result, I can also define the variable ...
3
votes
3answers
911 views
Variable not expanding inside another variable bash
I am reading a csv file in bash script as follows:
resource='/data/bscs/'
while IFS='|'read seqid fname fpath
do
echo "FILENO: $seqid"
echo "FILENAME: $fname"
echo ...
3
votes
5answers
6k views
How to use a shell variable inside sed's s command? [duplicate]
we know that we can get the value of a variable using $ sign:
x=3
echo $x
3
Is there other ways that we can use to get the value without using $ sign.
I'm asking this because $ sign is a special ...
3
votes
2answers
200 views
Command substitution declaration syntax in bash - which of these two is better practice? [duplicate]
This is part of a bash find loop, and I wondered which is more correct syntax and why?
filename="$(echo "$i" | cut -c5-)";
filename=`echo "$i" | cut -c5-`;
Both function for the purpose of getting ...
3
votes
4answers
2k views
Scope of variables when calling function from find
In a bash script I define a function that is called from find. The problem is that the scope of variables does not extend to the function. How do I access variables from the function? Here is an ...
3
votes
2answers
125 views
When should one use $( ) in defining variables
Some shell scripts I have come across use the following syntax when defining variables:
file_list_1=$(echo "list.txt")
or
file_list_2=$(find ./)
I would have used:
file_list_1="list.txt"
and
...
3
votes
2answers
894 views
creating variable using variable value as part of new variable name
I'm trying to create a new variable using the value of an existing variable as part of the variable name.
filemsg"$word1"=" "
I've also tried
filemsg$word1=" "
filemsg${word1}=" "
on all ...
3
votes
1answer
2k views
Linux: set command for local variables
I am trying to understand local/environment variables and export/set commands.
$ set FILEM="razrax"
$ echo $FILEM
$ FILEN="test"
$ echo $FILEN
test
Please explain why echo $FILEM returns empty ...
3
votes
2answers
1k views
Assign Subshell background process pid to variable
I am currently starting a background process within a subshell and was wondering how can I assign its pid number to a variable outside the subshell scope?
I have tried many different ways but MYPID ...
3
votes
3answers
60 views
Reuse similar flags for multiple commands
Let's say I want to execute several commands with similar flags such as:
du -sh / --exclude=/dir1 --exclude=/dir2 --exclude=/dir3/dir4
tar cvf archive / --exclude=dir1 --exclude=dir2 ...
3
votes
2answers
84 views
using the variable “file” obtained from `for “file” in` and pass to another script fails [closed]
I am trying search for all .mkv files in my current folder, then using mediainfo I want to put the height from its meta data to a variable, but it seems it is failing.
This snippet:
...
3
votes
2answers
178 views
Assign specific line from a variable to another variable
I would like to take a specific line from a variable to another variable.
I tried this but it doesn't work:
c="1.apple
2.banna
3.peach"
read "Please choose fruit [1-3]:" t
a=$c | awk "NR==$t"
echo ...
3
votes
2answers
305 views
How do you preserve a space in a literal expression in an array value in bash and how to trim down results from a sort command?
In the context of searching for expressions in log files, I was wondering generally if there was a way to quantify and qualify the contents of the logs in /var/log in some way. In particular, does the ...
3
votes
1answer
45 views
Store specific output to a shell variable
I am looking to store the output of the svn info command, more precisely the revision number into a variable for further processing. I just need the 4 stored. Can anyone help me please?
$ svn info
...
3
votes
1answer
86 views
Parameter expansion in variable assigned with a wildcard
I have two files in my current folder (MA502) whose names are -
MA502_TAAGGCGA-TCGCAGG_L001_R1_001.at.fastq
MA502_TAAGGCGA-TCGCAGG_L001_R2_001.at.fastq
I have many such folders - ex MA503, MA504 ...
3
votes
2answers
175 views
How to use tee to capture STDOUT from a code block to a filename defined within the block?
I want to send STDOUT for a script block to a file which name is defined by a variable within the block. However, when I use tee, it seems the variable outside the block doesn't exist anymore. Without ...
3
votes
3answers
75 views
How can I base the file name off of an already existing variable?
I'm using this to delete lines that don't match a pattern:
$ egrep "pattern1|pattern2|pattern3|pattern4|pattern5"
But now I need to do it automatically, so I'm setting a variable like this:
$ ...
3
votes
2answers
33 views
Setting variable with default value behaves differently when preceding a command?
Setting a variable with a fallback default value works...as long as we're using the variable in a subsequent command:
$ unset APP_ENV
$ echo $APP_ENV
$ APP_ENV="${APP_ENV:-production}"; echo ...