This question already has answers here:
What does 'cd ${0%/*}' mean in bash?
(2 answers)
What is the meaning of ${0%/*} in a bash script?
(1 answer)
Closed 6 years ago.
I have found this piece of code while studying a bash script:
dir=${0%/*}
I suspect the code inside the braces to be regex but I don't see what it means. Any idea?
It is not a regex, but it is a pattern match. It sets dir to the name of the script, which is $0, but without the last slash and any non-slash after it, if there is a slash in $0. If there is no slash in $0, dir gets a copy of $0 unchanged. See "Parameter Expansion" in the Bash Hackers Wiki.
Related
This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Closed 7 months ago.
To recreate this you will need to create a file named "l l.tex" in your home directory. Then make a an executable script with the following code
alacritty -e vim l\ l.tex
This opens the file, as I desired. Now create the following script
var="/home/l\ l.tex"
dar=$(echo $var | sed 's/\/home\//\#/g')
bar=$(echo $dar | sed 's/#//g')
alacritty -e vim $bar
This script, however, makes two new documents. However, they should be the same. I think there is an issue with how $bar is read. I am novice at this, but I reckon that this has to do with how strings are stored.
Can someone help me fix the second code so that I can use a variable?
This question already has answers here:
How to create shell variable with dashes?
(2 answers)
Closed 1 year ago.
I want to find the length of $0
If I use the following bash code
leading-space=${#0}
I get the following result:
./Install: line 25: leading-space=9: command not found
$0 has the value ¨./Install¨
The length of the string appears to be correct, but then bash gets confused. I am running bash 5.0.17 on Ubuntu 20.10. Can anyone see what I am doing wrong?
Identifiers in bash may not contain -, therefore the whole assignment is interpreted as a command (like cd, or grep) but there is no command with the name leading-space=9 on your system, resulting in the error.
Use the following:
leading_space=${#0}
This question already has answers here:
Echoing a tilde to a file without expanding it in Bash
(3 answers)
Pass a special variable (~ tilde) to Java program
(1 answer)
Closed 5 years ago.
I have a simple bash script that can accept arguments that it will be treating as text strings, nothing more.
If I give it ~, without quotes, then the home directory /home/users/me is what's parsed. Quote it, "~", it's fine. The character "~" is what I want, not the home path.
Is there any way I can ensure an un-quoted ~ is treated exactly as the character "~", not the home directory alias?
The bash shell is expanding the ~ on the command line before the argument is passed to your script.
There might be a bash option that you can change in your shell, but that would affect everything in your shell, which doesn't sound like what you want.
The short answer I think is no. There's nothing you can do in your script to change how the parent shell expanded any arguments before passing them to your script.
This question already has answers here:
Filename not printing correctly with underscore "_" in Bash [duplicate]
(2 answers)
Error in string Concatenation in Shell Scripting
(3 answers)
bash variable interpolation separate variables by a hyphen or underscore
(3 answers)
When do we need curly braces around shell variables?
(7 answers)
Closed 5 years ago.
#!/bin/sh -f
set proj_dir="OutputDir"
for projname in lib proj1 proj2
do
mv ./scripts/$projname_BYTECODE ./$proj_dir/scripts/$projname
done
A very simple example of what is not working well for me. $projname_BYTECODE is being interpreted as a variable name but _BYTECODE is actually part of the folder name. Suggestions?
Use ${X} instead of $X, so in your example ${projname}_BYTECODE should do the trick. Have a look at this question for more information: When do we need curly braces in variables using Bash?
This question already has answers here:
Trouble understanding parameter substitution in a script
(1 answer)
Usage of :- (colon dash) in bash
(2 answers)
Closed 9 years ago.
I've found this in a shell script that I use and I'm having trouble finding a formal description/definition of this syntax:
ACTION=${1:-update}
I'm assuming that if $1 variable does not exist (no command line arguments) then "-update" is used.
It's not esoteric. It's POSIX, and even Bourne. In every shell manpage ever. man bash or man ksh. The assumption is mostly right, if the parameter 1 is unset or empty string, then expand the alternate.
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02