The tag has no wiki summary.

learn more… | top users | synonyms

6
votes
4answers
562 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 ...
6
votes
5answers
252 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 ...
0
votes
2answers
44 views

Bash Mysql Command Substitution Login Failure

Bash and MySQL versions: GNU bash, version 4.2.25(1)-release (x86_64-pc-linux-gnu) mysql Ver 14.14 Distrib 5.5.37, for debian-linux-gnu (x86_64) using readline 6.2 I have a bash script that tries ...
4
votes
1answer
62 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 ...
2
votes
2answers
531 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 ...
4
votes
1answer
270 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
2answers
74 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=$? ...
2
votes
1answer
442 views

Storing whitespace in a shell script variable [duplicate]

I need to store specific number of spaces in a variable. I have tried this: i=0 result="" space() { n=$1 i=0 while [[ "$i" != $n ]] do result="$result " ((i+=1)) ...
3
votes
5answers
319 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 ...
6
votes
3answers
216 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 ...
3
votes
2answers
148 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 ...
1
vote
1answer
63 views

How can I use global variable arrays in aliases or functions

I am attempting to simplify repetitive work procedures. In doing so, I am trying to write a .bashrc script which will set global path variables that aliases can refer to. Disclaimer: I'm new to linux ...
5
votes
1answer
297 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 ...
2
votes
1answer
68 views

Could “>” be included in a bash Variable?

Current approach: sudo awk 'BEGIN{printf "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<hello>\n\t<world>\n";} {print "\t\t",$0}END{printf "\t </world>\n</hello>\n"}' ...
1
vote
1answer
381 views

command not found in shell script

Please tell me why I couldn't print variable value. # chmod 777 chkscript.sh # ./chkscript.sh chkscript.sh file content variable = "This is variable" echo "$variable" echo "Hello World " ...
1
vote
1answer
27 views

Insert variable and keep the variable

I'm on VMWare which, no matter what anyone says, responds to most unix commands. Some things are unique though, for example the fact that almost no files are persistent through reboot. Due to this ...
2
votes
1answer
143 views

What is the difference between filename=${1:-/etc/hosts} and filename=/etc/hosts? [duplicate]

What is the difference between filename=${1:-/etc/hosts} and filename=/etc/hosts? For example: filename=/etc/hosts if [ -r "$filename" ] && [ -s "$filename" ]; then md5sum $filename ...
2
votes
3answers
41 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 ...
1
vote
2answers
85 views

list files and store it in variables

im new with shell programming. my goal is to give the user a selection which file should be run. i catch all available scripts in one var with: var=$(find script*) so far so good.. this is the ...
0
votes
1answer
220 views

Bash Script : Passing a variable to a bash script that contains quotes, single quotes. etc [closed]

lets assume this is the string: 'a',"b" it contains both single and double quotes. how would you pass this to a bash script as a single string ? this is the bash script: #!/bin/bash echo $1 ...
1
vote
0answers
25 views

How to combine Bash's read with HERE-document when shopt -os errexit is in place? [duplicate]

I am trying to make use of the solution presented in one of the answers of http://stackoverflow.com/questions/1167746/how-to-assign-a-heredoc-value-to-a-variable-in-bash In Bash version ...
4
votes
1answer
680 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 ...
5
votes
1answer
73 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 ...
5
votes
5answers
326 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 ...
2
votes
1answer
114 views

Value assigned inside a function variable is alway empty

I have a following simple script. In this script, I am assigning value to a global variable inside a function. I can clearly see that the value being assigned to the variable via debug statement. ...
1
vote
2answers
595 views

How do I get a variable's value from one script and import it in another script?

How do I get a variable's value from one script and import it in another script? But the catch is that the script where I am going to get the variable's value has also some commands that I don't want ...
0
votes
2answers
46 views

How to create a variable $ldir at your home directory that contains the relative path to letters?

If there's a directory on your Desktop called letters: 1) How to create a variable $ldir at your home directory that contains the relative path to letters? 2) How to copy a text file f1.txt to the ...
2
votes
1answer
72 views

Capture symbolic link into Unix/Linux variables

Say I have a folder with a series of symbolic links: ussdv103:en195d; default$ pwd /oradba/app/oracle/admin/database1 ussdv103:en195d; default$ ls -ltr exp -> ...
1
vote
1answer
237 views

Using a Counter Loop for Environment Variable

I'm trying to write a code that moves files to a .junk directory using the built-in environment variables. My goal is that all a user would have to do is type sh junk filename1 filename2 filename3... ...
10
votes
4answers
2k 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 ...
2
votes
2answers
1k views

Assign value to a variable in bash, coming from another commands

Doing this in bash, makes SUFFIX variable holding /dev/fd/[fd], SUFFIX=<( echo "&*645\[]}#@name" | sed -r 's/[^[:alnum:]]+/_/g' ) What should I do to assign the actual result, of commands ...
5
votes
4answers
5k 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 ...
6
votes
2answers
654 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 ...
1
vote
1answer
141 views

How can I use a loop inside a command?

I'm writing a shell script that contains a command that takes multiple directories as space-separated input arguments (like ls). I would like to fill in those arguments from a variable but don't know ...
2
votes
2answers
2k views

Read non-bash variables from a file into bash script

My bash script needs to read the variables from a text file that contains hundreds of variables, most of which conform to standard bash variable sytax (e.g., VAR1=VALUE1). However, a few lines in this ...
1
vote
1answer
32 views

Need to set a variable with “[]”

I have a tcsh script, in which I need to set a variable to this value defining my system type. type=abc project=def sysname=["Linux"] I tried various combinations to escape the [ and " but have ...
3
votes
4answers
198 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 ...
-3
votes
1answer
88 views

why does the dollar sign cause this command to be executed?

test_script.sh: #!/bin/sh D1="$(dirname $0)" echo $D1 D0="(dirname $0)" echo $D0 What I type on the command line: $ ./test_script.sh Output: . (dirname ./test_script.sh) What significance ...
3
votes
2answers
142 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 ...
2
votes
2answers
79 views

Nature of the positional parameters

I am beginner to shell scripting and wrote this script which sort of confused me a bit... function func { the variables received are echo $0: $1 and $2 } echo in the main script func ball boy The ...
2
votes
1answer
122 views

BASH_ALIASES not equal to alias

I've just recognized that (in Debian 7, within Bash 4.2.37) the output of BASH_ALIASES isn't equal to that of alias – which, according to gnu.org's Bash Manual, should be the case: ...
8
votes
1answer
1k 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 ...
-1
votes
2answers
3k views

Append variable string to itself [closed]

I have a variable into a do loop. I append its content to a file, but I want to append it to itself so I could use it to do other stuff like sending mail. I tried variable+=$variable but it didn't ...
1
vote
0answers
192 views

Shell script to backup VMware VM's, exporting each VM to a list then asking user intput

What I'm Trying To Accomplish: Get list of Virtual Machines ".vmx" file and place in variables Give the user and choice of which VM to backup Use "ovftool" to produce an export of the chosen VM to ...
0
votes
1answer
363 views

Test variable if its string or not

I'm writing a script which will have some arguments and so I am using getopts but i want to solve the problem with one argument. I use a switch, for example -d, and I want the argument for -d will ...
2
votes
3answers
2k views

Pipe assigns variable

For simplicity I would like to do echo cart | assign spo; echo $spo Output: cart Does such an appliance exist such as assign? I am aware of all the ways to do this using substitution.
2
votes
1answer
130 views

Appending to same array in various loops, only last values remain Bash 4

I have something like this: FILES=() for i in *.map do FILES+=($i) done find /var/candy -name "chocolate_[0-9]" | while read snack do FILES+=($snack) done for file in ...
2
votes
2answers
181 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
69 views

What does readonly mean or do?

What does readonly mean or do in this case? I have never seen or heard of it. a="testString"; b=a; readonly b; b=25; Also, would the final value of b at the end be 25?
1
vote
2answers
211 views

Execution of a command in a variable with eval

I am trying to use grep to color some log files . I wrote a script and a part of it looks like this : #!/bin/bash com="GREP_COLOR=\"1;36\" egrep --color=always '[^a-zA-Z0-9]' $log |less -R" ...