Bash -Handle double quotes in variable - bash

I am Declaring a variable
var1="D:\TEMP\scripts\test.sh"
echo var1
output:
D:\TEMP\scripts\test.sh
I would like to get the value from var1 and assign to another variable var2 and if I do a echo var2, I should be getting the outting
echo var2
Expected output:
"D:\TEMP\scripts\test.sh"

Sorry ..I got this already. Thought it would be difficult.
[user#server ~]$ var1="D:\TEMP\scripts\test.sh"
[user#server ~]$ echo $var1
D:\TEMP\scripts\test.sh
[user#server ~]$ var2=\"$var1\"
[user#server ~]$ echo $var2
"D:\TEMP\scripts\test.sh"
[user#server ~]$

You'll have to escape the quotes inside the quotes of variable; try this:
var1="\"D:\TEMP\scripts\test.sh\""
var2=$var1
echo $var2
output:
"D:\TEMP\scripts\test.sh"

Related

Bash add characters before and after $foo

So if I have
$foo = foo
I would like to add : at the beginning and ! at the end. I tried the below:
bar=$( ":" $foo "!" )
You can't use bar=$( ":" $foo "!" ), with this command you try to assign the result of :foo! command to your variable. You will probably get an output like :foo!: command not found and your $bar variable will be not set.
You can use :
foo=foo
bar=':'$foo'!'
echo $bar
# Will return :foo!
the builtin printf can do that also but I'm not sure if it is portable solution.
foo=foo
printf -v bar ":$foo!"
echo "$bar"
see
help printf
or
man printf

How to remove the character before the pattern found? (Unix)

I have a variable:
$var="-- ---comment- --
abcd;"
I want to remove every character that is between "--" comment "--"
Expected output:
$var="abcd;"
What I am trying in test.sh
#!/bin/bash
var="----comment---
abcd;"
test=$(sed 's/^--.*--/' $var)
echo $test
It is not working. I get the following error:
command not found
Trying to replace the pattern that you don't expect in output with nothing, something like this
test=$(echo $var | sed 's/^--.*--//g')
Should work.
In bash:
$ cat foo.sh
var="----comment---
abcd;"
echo "$var" # this outputs the original var
echo "${var/--* /}" # this output replacing everything between -- and " "
$ bash foo.sh
----comment---
abcd;
abcd;
You can set the output to a variable by
$ var="${var/--* /}"

UNIX Script - setting dynamic variables (indirect variable reference)

How to set shell variables from an input file ?
hello,
I need to set dynamic variable from an .ini file in a shell script.
Assume the input file is input.ini :
var1=val1
var2=val2
var3=val3
In a script I want to set var1, var & var3 respectively to their val1, val2 & val3 to get
echo $var1
val1
echo $var2
val2
...
I've tryed :
file="input.ini"
while IFS== read -r f1 f2
do
eval dynvar=$f1
dynvar=$f2
done <"$file"
echo $var1
echo $var2
echo $var3
the echo $varx commands give no output. How can I work it out ?
thanks in advance.
source input.ini
Or
. input.ini
More info
<source | .> filename [arguments]
Execute commands from a file in the current shell.
Solved
using :
file="install.ini"
while IFS== read -r f v
do
eval "$f=$v"
done <"$file"
did the trick.

Bash: Spliting string based on some delimiter and storing each in a variable

113050050/CS101/mysql_java.pdf
the above is my string, which is stored in a variable 'line'
line="113050050/CS101/mysql_java.pdf"
Now I want to split $line based on delimiter / and store each single part in a variable
var1=113050050
var2=CS101
var3=mysql_java.pdf
$ IFS=/ read var1 var2 var3 <<< "$line"
Results
$ echo $var1
113050050
$ echo $var2
CS101
$ echo $var3
mysql_java.pdf
This might work for you:
line="113050050/CS101/mysql_java.pdf"
var=(${line//\// })
var1=${var[0]}
var2=${var[1]}
var3=${var[2]}
as #chepner points out this will fail if spaces exist in the $line variable, a perhaps more bullet-proof solution is to use the IFS variable:
line="113050050/CS101/mysql_java.pdf" O="$IFS" IFS='/' var=($line) IFS="$O"

BASH: Substituting a variable inside a variable during echo

I explained my question in the comments:
VAR=
INS="Installing $VAR"
echo $INS
. # In each echo command I want to dynamically substitute
. # the $VAR variable in the $INS variable. I want to do
echo $INS # the substitution of the variable on echo command.
Is this possible?
You need a function to do the job gracefully.
say() {
echo "Installing $INS"
}
INS=hello
say
INS=world
say
Or just this:
say() {
echo "Installing $#"
}
say hello
say world
For example:
[ghoti#pc ~]$ cat varins
#!/bin/bash
msg='Installing "$VAR"'
for VAR in foo bar baz; do
eval echo "$msg"
done
[ghoti#pc ~]$ ./varins
Installing foo
Installing bar
Installing baz
[ghoti#pc ~]$
This relies on the fact that $VAR won't be expanded inside single quotes. The eval command will expand the $msg variable, in which the shell will find $VAR to replace.
Parameter substitution can do it also:
ins='aaa $var aaa'
var='xxx'
echo "'${ins//\$var/$var}'"
result:
'aaa xxx aaa'
INS='Installing $VAR'
result=`eval "echo $INS"`
echo $result
you have to use single quotes to not substitute $VAR on creation of INS
eval evaluates $INS at runtime and with echo and backticks it is returned as substituted string
As Hachi said, you need single quotes to prevent $VAR from premature evaluation. Graham suggests in the comments to avoid the subshell call like this:
#!/bin/bash
VAR=17
INS='Installing $VAR'
eval "echo \"$INS\""
VAR=42
eval "echo \"$INS\""
If you don't do it:
VAR=23
INS="Installing $VAR"
echo $(eval "echo $INS")
VAR=8
echo $(eval "echo $INS")
23 is bound immediately, and the assignment VAR=8 isn't noticed.
Result:
Installing 17
Installing 42
Installing 23
Installing 23

Resources