Bash Scripting

I had a couple of questions that I was hoping someone could help me with?

  1. chmod +x script.sh
  • where exactly should we declare this (within the file or terminal because in the exercise to do with bash scripting (web development career path) it wasn’t used at all).
    • PATH=~/bin:$PATH
  • what does this actually mean and how comes it wasn’t used in the exercise? I think what it’s trying to delcare PATH (an environment variable containing scripts for bash to run) as the location of the script file, but I don’t understand the use of $PATH.

Altering permission with chmod can be used inside a script or through the terminal depending on what it is you’re doing. If you’ve written a single script and it cannot be executed then you’d probably be altering it through the terminal.

In your second query so far as I’m aware that would add your new location to the start of your path (it’s searched before anything else, which might not be ideal). You have the new location, ~/bin and the separator : followed by $PATH which would reproduce your current PATH.

It’s uncommon to be altering the path itself. I’m assuming that you’re creating your own directory for scripts on your own PC. It’s up to you whether you wish to do so. If you don’t call these scripts often then there’s no pressing need to be altering your PATH at all.

2 Likes

So I just add chmod + x only if my code script doesn’t run due to permission right?

And secondly, does * PATH=~/bin:$PATH basically mean I’m redeclaring the PATH environment variable by adding the bin directory to the rest of the directories in PATH (which is accessed using $PATH). Is this because directories in PATH are simply separated by ‘:’ or does it mean anything else? In which case, does ~/bin already exist in PATH anyways?

Thanks a lot for your help btw! I’m just a little confused haha.

It varies a bit with your set-up so the best way to know is to test it. Checking permissions is fairly straightforward with ls -l and you can find out whether what permission flags are set and whether you actually want to permit execution for everyone or not (default is chmod a + x, check the man page for by user/group etc.).

So far as I know that’s pretty much it, I’m not aware of anything special when it comes to altering the PATH like this (that doesn’t mean there isn’t one, I’m just unaware of it). Even when it’s in your bashrc file it doesn’t permanently edit the path, it just tags on your additions every time you open a bash shell. The colon is necessary as a text separator but I don’t think (once again I can’t say this with certainty) it has a special purpose beyond this. You might need to start nosing through stackoverlfow queries or similar to know with 100% certainty.

As for what already exists in the variable, why not find out with echo and then you can decide what to add on.

2 Likes

thanks a lot man!!!

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.