Wildcard in variable, Shell bash, how to do ? Variable=$variable2* [duplicate] - bash

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed 4 years ago.
For me it worked:
diretorio=$(echo 'test 123'*)
but not worked when i used variable in quotes
Var2="test 123"
diretorio=$(echo '$Var2'*)
How to solve it?

The mistake in your glob is that
diretorio=$(echo '$Var2'*)
is a shot in /dev/null, because the shell don't expand variables in single quotes.
So :
diretorio=$(echo "$Var2"*)
Learn the difference between ' and " and `. See http://mywiki.wooledge.org/Quotes and http://wiki.bash-hackers.org/syntax/words

May I suggest an alternate approach? Instead of making a space-separated list of filenames (which will cause horrible confusion if any of the filenames contain spaces, e.g. "test 123"), use an array:
diretorio=("${Var2}"*)
doSomethingWithAllFiles "${diretorio[#]}"
for umDiretorio in "${diretorio[#]}"; do
doSomethingWithASingleFile "$umDiretorio"
done

Use double quotes:
diretorio=$(echo "$Var2"*)
Single ones prevent variable substitution

Related

Prevent variable expansion in Bash heredocument [duplicate]

This question already has answers here:
How to cat <<EOF >> a file containing code?
(5 answers)
Using variables inside a bash heredoc
(3 answers)
Escaping a dollar sign in Unix inside the cat command
(2 answers)
How to split strings over multiple lines in Bash?
(12 answers)
Closed 4 months ago.
I would like to create a longer multiline string in Bash without variable expansion. This text contains also items like ${varname} which is expanded according to the specification. However, I don't want to expand them in my script.
Dockerfile=`cat <<____
ARG BUILD_IMAGE
FROM ${BUILD_IMAGE}
...
# lots of further lines
____
`
I tried several variants ($\{, $\0173), but I could only solve it with
b='${'
...
FROM ${b}BUILD_IMAGE}
...
Which is really ugly. Is there any better way to solve this?
Edit:
Dockerfile=`cat <<'____'
ARG BUILD_IMAGE
FROM ${BUILD_IMAGE}
...
# lots of further lines
____
`
would solve the issue, but in this case I can't use any variable expansion.
Edit 2 - Answer:
Since the post has been closed, I answer my question here:
There is a difference between using backquote and $(. The issue is solved if I use the latter one. I had the problem, because I used the former one.
When the old-style backquote form of substitution is used, backslash
retains its literal meaning except when followed by $, `, or . The
first backquote not preceded by a backslash terminates the command sub‐
stitution. When using the $(command) form, all characters between the
parentheses make up the command; none are treated specially.
This means in praxis:
text=$(cat <<__EOT__
Hello ${who}, hello \${who}!
__EOT__
)
echo $text
text=`cat <<__EOT__
Hello ${who}, hello \${who}!
__EOT__
`
echo $text
results in
Hello World, hello ${who}!
Hello World, hello World!

How to add $USER inside of nested quotes? [duplicate]

This question already has answers here:
Difference between single and double quotes in Bash
(7 answers)
Closed last year.
I have a variable:
my_var="$('/some/path/to/username/more/path' 'and/something/else')"
and I want to change it to be more generic like:
my_var="$('/some/path/to/${USER}/more/path' 'and/something/else')"
but that doesn't work. How can I get the user inside of there?
The problem is that inside a single quoted string nothing is treated with a special meaning. This makes them good for scripting, eg. grep '^pattern$' ... and sed 's/abc$/def/'.
You can use double quotes instead.
$(...) is a command substitution. Is that correct? If so, then you can nest double qoutes:
my_var="$("/some/path/to/${USER}/more/path" 'and/something/else')"
This should be interpreted as two set of quotes, so the outer double quotes, isn't stopped at the first inner quote:
my_var="$( )" # First set of quotes
"/some/path/to/${USER}/more/path" # Second set of quotes
When assigning a variable, you don't need to wrap the expression in double quotes, so:
my_var=$("/some/path/to/${USER}/more/path" 'and/something/else')
would also be fine in this case. Most other cases you should always wrap parameter expansions ($abc), command substitutions ($(...)) etc. in double quotes as to avoid word splitting and pathname expansions.
However to me it seems like you are trying to create an array instead?
$ my_var=("/some/path/to/${USER}/more/path" 'and/something/else')
$ echo "${my_var[0]}"
/some/path/to/and/more/path

bash variable eats multiple spaces, turning them to one [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 3 years ago.
# export var="many spaces"; echo =${var}=
=many spaces=
What is going on here?
Why multiply spaces are turned to one? How to keep all?
You’re simply missing quotations around your variable. Changing your code to this:
$ export var="many spaces"; echo ="${var}"=
=many spaces=
should give the result you’re looking for. One “feature” of bash that you need to watch out for is word splitting, which is based on the value of your IFS (internal field separator) variable. Typically IFS defaults to
IFS=$' \t\n'
so you need to take care in quoting variables that contain spaces, tabs, and newlines.

SHELL add double quotes at the beginning and end of specific variable [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
When to wrap quotes around a shell variable?
(5 answers)
Closed 4 years ago.
I have variable in shell script which takes a value in variable $var1.
If I do echo $var1 I will get for example value Boston.
My desired value is "Boston"
I tried couple of cases:
var1=""$var1""
var1="""$var1"""
But in both cases I am getting as result '$var1' but I want "Boston"
Please for solution do not mention solution which contains Boston in it. I would appreciate if you can use only var1 in the whole script so it can be more clear! "Boston" just needs to be output of the script execution and for initiliztion for the variable var1.
Thanks
Escaping quote sign with backslash is what you need
var1=Boston
var1=\"${var1}\"
echo $var1
"Boston"
Additionally, curly braces is safe notation for variables

UNIX Replace string without replacing space [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 5 years ago.
For string matching purposes I need to define a bash variable with leading spaces.
I need to define this starting from an integer, like:
jj=5
printf seems to me a good idea, so if I want to fill spaces up to 6 character:
jpat=`printf " %6i" $jj`
but unluckly when I am trying to recall the variable:
echo $jpat
the leading whitespaces are removed and I only get the $jj integer as it was.
Any solution to keep such spaces?
(This is equivalent to this: v=' val'; echo $v$v. Why aren't there leading and multiple spaces in output?)
Use More Quotes! echo "$jpat" will do what you want.
There is another issue with what you're doing: Command substitutions will remove trailing newlines. It's not an issue in the printf command you're using, but for example assigning jpat=$(printf " %6i\n" "$jj") would give you exactly the same result as your command.

Resources