Concatenating asterisks in bash [duplicate] - bash

This question already has answers here:
Printing asterisk ("*") in bash shell
(2 answers)
Closed 7 years ago.
I am trying to concatenate asterisks (*) to a string variable. However, I keep getting the files in the current directory instead. I have tried
row+='*'
row+=$row #where row is *
row='*'"$row"
row="\*$row"
row="${row}*"
etc.
row='*'."$row" #produces *.*.*.
I thought \ would escape the *, but it didn't work.

Try using quotes around the variable before you "print".
For example:
cronSen="*/$a * * * * bash /etc/init.d/ckDskCheck.sh"
echo "$cronSen"
This could work. I got the idea from here: Printing asterisk (*) in bash shell

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!

native bash way to add trailing whitespaces to variable [duplicate]

This question already has answers here:
Create string with trailing spaces in Bash
(2 answers)
Bash LeftPad String with spaces inside variable
(1 answer)
Closed 2 years ago.
Is there a pure bash way to add trailing whitespaces with something like parameter substition in the example above I am using printf in conjunction with command substition witch is not that performant
declare -ir _CONST_VARIABLE_LENGTH='30' _CONST_SUBTRACTOR='3'
declare some_var='here is a string'
declare new_var
new_var="$(printf "%-$((_CONST_VARIABLE_LENGTH-_CONST_SUBTRACTOR))s" "$some_var")"
# what i want, but doesn't work
# ${var:0:LENGTH} only goes till actually length and won't add something if LENGTH is greater than actual var lenght
new_var="${some_var:0:$((_CONST_VARIABLE_LENGTH-_CONST_SUBTRACTOR))}"

Why does * put all file names in the argument vector? [duplicate]

This question already has answers here:
What is file globbing?
(1 answer)
Stop shell wildcard character expansion?
(4 answers)
command line * linux [duplicate]
(1 answer)
Closed 4 years ago.
I stumbled upon this while working through the exercises in K&R2. Why does echo * prints the names of all files in the current directory? More generally, when I write a C program that takes command-line arguments, and when I give it * as an argument, it puts the names of all files in its parent directory in to the argument vector. Why does this happen? What is so special about *?
I could not find anything about this in the internet.
This is called globbing. Here's a detailed description. Other wildcards include ? for one character, [abc] for one of a set of characters, and [a-z] for one of a range of characters. This is built into various shells, including Bash.
In response to your comment "I think echo is written in C" — this doesn't matter a bit. Once source code is compiled into an executable containing machine code, it doesn't matter what language it was written in.

Double expansion of a parameter in Bash script [duplicate]

This question already has answers here:
Lookup shell variables by name, indirectly [duplicate]
(5 answers)
Closed 7 years ago.
I want to expand a parameter twice, and ${$PARAM} doesn't work.
For example, if I set two variables to file names and then want to loop through those variables and expand to their file names:
INPF_1=input1.inp
INPF_2=input2.inp
# copy the input files to the execution directory
for input_param in ${!INPF_*}; do
echo ${$input_param}
done
How can I to access the file names from those parameters in the for loop?
I.e., expand to input1.inp and input2.inp.
You had it almost: just use "${!input_param}" instead of ${$input_param}.
The quoting doesn't do anything in this case, but it's a good habit.
Be aware of the difference to ${!INPF_#}, which – when used between double quotes – expands to a separate word for each variable name, whereas "${!INPF_*}" doesn't. You almost always want "${!INPF_#}". See the difference:
$ for var in "${!INPF_*}"; do echo "$var"; done
INPF_1 INPF_2
$ for var in "${!INPF_#}"; do echo "$var"; done
INPF_1
INPF_2
See the manual for the various parameter expansions.

why we use ##*/ expression with bash variable [duplicate]

This question already has answers here:
explain the linux regex for getting filename
(2 answers)
Closed 8 years ago.
I am tring to understand the bash script.
I am seeing ##* / expression with bash variable.
i.e ${foo##*/}
Can someone please tell me why we use that expression?
It's called "Parameter expansion". The variable $foo is searched for a substring matching the pattern */ (i.e. anything up to a slash) from the beginning (#), and what remains in the variable is returned. Doubling the #-sign makes the matching greedy, i.e. it tries to find the longest possible match.

Resources