This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 3 years ago.
Based on this answer: https://stackoverflow.com/a/4141042/226473
I came up with this script:
ALT02884% for i in {0..4}
do
printf "Set for $1" > "$i_directories"
done
But it results in the following:
zsh: no such file or directory:
zsh: no such file or directory:
zsh: no such file or directory:
zsh: no such file or directory:
zsh: no such file or directory:
The shell thinks the underscore is part of the variable name. Use curly braces to tell it to look for a variable named i rather than i_directories.
for i in {0..4}
do
printf "Set for $1" > "${i}_directories"
done
Related
This question already has answers here:
Why does my Bash code fail when I run it with 'sh'?
(2 answers)
Closed 10 months ago.
I am kind of new in shell scripting and trying to learn arrays. I declared array value but when I am trying to print that array it is giving me an error(bad substitution).
I am pasting the code below, please suggest to me what is wrong here-
➜ ~ cat test.sh
#!/bin/bash
array=['foo','bar','a','b']
echo 1
echo "${array[0]}"
➜ ~ sh test.sh
1
test.sh: 5: Bad substitution
Thanks in advance.
Depending on the system you're using sh might be not be Bash and
it's not Bash on yours, it can be dash for example. Run your script with Bash:
$ bash arr.sh
1
[foo,bar,a,b]
Or set an executable bit and call the script without providing the name of the interpreter since you already have the shebang:
$ chmod +x test.sh
$ ./test.sh
1
[foo,bar,a,b]
This question already has answers here:
Extract filename and extension in Bash
(38 answers)
Closed 2 years ago.
I need to get all file names for a directory
for file in "$1"/*;do
echo $file
done
gives me directory_name/file1_name, directory_name/file2_name.
However, I just want the file1_name
You can use parameter expansion to get only the filename before printing as follows:
for file in "$1"/*;do
file="${file##*/}"
echo $file
done
You can find more about parameter expansion over here : Shell Parameter Expansion (Bash Reference Manual)
This question already has answers here:
What's the point of eval/bash -c as opposed to just evaluating a variable?
(3 answers)
The 'eval' command in Bash and its typical uses
(11 answers)
Closed 2 years ago.
There is something i do not understand with strings in bash:
Look at this script:
#!/bin/bash
tmp="ls"
"$tmp"
This script executes ls command and display result in the console.
Now look at this script:
#!/bin/bash
tmp="ls > out.txt"
"$tmp"
This second script does not execute ls and displays this error:
line 3: ls > out.txt: command not found
I just want to understand. I do not want to understand how to run ls command. I want to understand why the first script works and not the second.
Thanks
This question already has answers here:
How do I get the directory where a Bash script is located from within the script itself?
(74 answers)
Closed 4 years ago.
If I'm in /home/whatever and I run, say, $ bash /home/folder/subfolder/script1.sh, how can I get /home/folder/subfolder into a variable? $0 or $(readlink -f $0) give me the whole path and filename, i.e. /home/folder/subfolder/script1.sh, which isn't what I'm after. Thanks in advance.
Use basename after getting the script name
dirname /home/folder/subfolder/script1.sh
outputs
/home/folder/subfolder
You may try this:
#!/bin/bash
p=$(dirname $0)
echo $p
This question already has answers here:
What do $? $0 $1 $2 mean in shell script? [duplicate]
(2 answers)
Closed 5 years ago.
I have seen one shell script starting with the below code :-
#!/bin/bash
currfoldername=$1
cd $currfoldername
can anyone describe what does $1 mean here?
Thanks for your reply!!
$1 means the first argument given while executing the shell script.
Example -
# my_script.sh
#!/bin/bash
currfoldername=$1
cd $currfoldername
echo "in $currfoldername"
execute -
./my_script.sh my_folder
output -
# value of variable currfoldername is my_folder.
#cd to my_folder
in my_folder # echo statement