what does the below shell script mean? [duplicate] - shell

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

Related

Is there a way to see the action of a shell script as it is executed? [duplicate]

This question already has answers here:
How can I debug a Bash script? [closed]
(12 answers)
Closed 1 year ago.
I am pretty new here and learning programming.
I wonder if there is a way to see the action of a shell script as it is executed?
For example, I want to see how the values are inserted into the variables in this script.
#!/bin/bash
# Script: potenz2.sh (powers of two with a while loop)
i=0
j=1
while (( i < 12 ))
do
j=$((j * 2))
echo -n "$j"
i=$((i+1))
done
echo ""
Put set -x at the beginning of the script, or run it with that option
bash -x potenz2.sh

bash - get directory a script is in when run from a different directory [duplicate]

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

bash sh script always executes regardless of parameter [duplicate]

This question already has answers here:
How do I compare two string variables in an 'if' statement in Bash? [duplicate]
(12 answers)
Closed 4 years ago.
I have the following script
#!/bin/bash
if [ $1=="1" ]
then
echo $1
fi
Whenever I run ./myscript.sh 0 it still prints "0". I am not sure why? It prints whatever I type in because the if executes. What would I need to change?
Add proper spaces, i.e. before and after == inside if condition
#!/bin/bash
if [ $1 == "1" ]
then
echo $1
fi

source command output lines [duplicate]

This question already has answers here:
How to echo shell commands as they are executed
(14 answers)
Closed 4 years ago.
I want to source a file containing several commands in a bash shell. How can I have the currently executed command printed on top of the commands output?
E.g. given this file test.sh
echo "hello"
echo "world"
the output of source test.sh should be:
echo "hello"
hello
echo "world"
world
Type set -x before source test.sh. This tells the shell to show the commands that are being executed before executing them.
Type set +x to undo it afterwards.

Shell script (assign to variable) not working [duplicate]

This question already has an answer here:
bash script with variables not getting set [duplicate]
(1 answer)
Closed 2 years ago.
#!/bin/bash
if [ -f "$1" ] #parameter is file name, if i exists...
then
VARIABLE=`cat "$1"` #assign what's inside into VARIABLE
fi
I've tried bash -x script_01.sh "file" and the tracing works:
+ '[' -f file ']'
++ cat file
+ VARIABLE='Wednesday, November 4, 2015 04:45:47 PM CET'
But the problem is that when I echo $VARIABLE, it's empty. Do you guys have any tips on how to make it work? Thank you.
VARIABLE is set in the process that runs the script, not the process which called the script. If you want to set it in your current environment, you need to source the file instead.
. script_01.sh "file"

Resources