Bash Scripting 4 -- How Bash Scripts Work My notes: What command do you use to find out where the Bash binary is? (hint: The first thing you declared in every script) which bash (will output /bin/bash or other location it is stored in) - When you create a script, why should you use the file ending .sh? (even though file endings won't matter to the shell?) Because the it will be easier for the people who come after you to understand what it is - Return values can be anywhere between 0 through.... ..255 - When you use 'exit' at the end by itself, what will the shell do? It will look for the last item that was called and will return that as the program's return value (as long as it returns 0/has no errors) - When you use 'exit $?', what are you doing in your program? You are showing that you are intentionally exiting and telling the program to output the last item that was called as the output - When you execute a file, what permission do you need to make sure that you have? execute (use chmod on the file to allow execution of it) - Running a program in the shell: Why should you use: 'source helloworld.sh' rather than 'bash helloworld.sh' ? Because you will have access to the variables and information stored inside the script in your current bash session (makes it easier because you won't have to run things over and over again to modify certain elements) - What is the difference between these two: source helloworld.sh . helloworld.sh There is no difference, both are interchangeable - Tell me if there is anything that I missed or got wrong. Have a safe and productive day.
Hi, Sorry ignore my question. Just watching a few seconds more gave me the answer. It is at the kernel level as you said. I never knew that before! Thanks
Wanted to know what if changing the $message variable after sourcing the script would actually change the variable in the script or just in the shell session. Turns out it just changes the value of the variable in the login shell and not the script. Sourcing the script again will reset $message variable in the login shell to the value specified in the script. Great stuff!!
Hey, I was following along fine but for some reason now I'm not able to execute anything. What have I messed up. Sorry, user is not allowed to execute '/bin/bash' as root on hostname
Very nice videos. Something that I never understood. Why "#!/bin/bash" is so important in the first line of a script if the "#" starts a "comment line" that is "ignored"? What is the process behind the scripting language that requires that specific line? I would understand if there wasn't any "#" first. Thanks
+linuxmq when your machine executes a script, it is read both by the os and the particular programming language that does the executing. the os comes first, reads the first line, sees the she-bang, and sends the file to the particular programming language binary, the programming language sees the # and presumes that the particular line is useless, just another comment.
I just did a test and found out that only "./test.sh" reads the interpreter in the 1st line. I found this by mistaken the bash's path. All other 3 options, that is ". test.sh", "source test.sh" and "bash test.sh" have returned the expected output, whilst the 4th one returned "bash: ./test.sh: bin/bash: bad interpreter: No such file or directory"
Thanks; I could have explained that better. Just remember that it shows you the exit status of the last command. If you play with it in the shell it'll make sense quickly.
Because he is making sure the concepts are driven home. Do take the time to read the description and titles of playlists before you comment items like this. This video is located in a playlist titled "The Linux Basics Course". Keyword there is "Basic"