Starting Bash Scripting

Reference exercise

Question 1:

When should I put this command?

chmod +x

In my file or in my whatever-code-I-wrote-that-need-it file (I will later refer to as code_file)?
If in my code_file, is this how to use it? (I wrote it according to my understand at this moment.)

#inside a code file
#file name: code_file
chmod +x
#insert code


#type this code in terminal 
chmod +x 
#before I run my code file

Question 2:

I don’t understand how is “bash scripting” differ from “bash profile”. I mean they both store user’s frequently use commands and help user shorten their commands when they have to use the same things repeatedly.

Question 3:

Does bash scripting is a new file recommended to user to create or does the file have already exist in our system waiting for us to make change?

Question 4:

What are some examples of when to use bash scripting vs bash profile? Including creating the environment. In my head I kinda get the concept, but I still can’t quite able to come up with a solid example to myself. When I think this concept should apply for bash scripting for one moment, another moment I came up with something different which may or may not relate to my previous understanding. Some can explain one functionality of one way to use it, but not enough to explain the whole things.

Question 5:

Relate to question 4, but talk about how would it be different in each OS, mac, linux (in general since it has many distros and each one is kinda unique), windows.

I would really appreciate your clarification.

Hey @discreteloner! In answer to your first question, you aren’t supposed to use chmod +x, you are supposed to use #!/bin/bash.

1 Like

I did not use it in the exercise; I saw it mentioned in the explaining part and did not see that line appear anywhere in the exercise so I got curious of when to use it.

1 Like
  1. you need chmod to make your script executable, have you learned about file permissions in linux yet? chmod command needs to be run on the file itself, so it shouldn’t be part of the script

  2. separation, if you have dozen script with a lot of lines (maybe even a hundred), you don’t want to stuff all of that in one file. bash profile is useful if you have long one liners you want to alias to shorten them. even then, i would put the aliases in a separate file, so you keep bash profile for other useful linux configuration.

  3. Both. You can create new script or update an existing one

  4. bash profile is a special file, as explained here. so each time you login into a linux system, .bash_profile is read so its quite useful for login configuration. on the other hand, a script could be fetching background image from national geographic every 15 minutes.

  5. macOs and linux both use POSIX standards, so bash scripts work on most linux distros and macOS. Windows does not support bash (well, there are solutions like WSL), so for scripting on windows you will need powershel. I think this course would be good for you. That course focus more on the questions you have then the codecademy course.

1 Like

Answer for questions

I have not learn about file permissions in linux. Even if this is the case, I understand the concept of it from what you said even though I cannot know how to use it.

So, does this mean .bash_profile is used when I want to write shortcuts for my frequently use command, while a script contains code that I can make it do something other than be a shortcuts reference such as use it to fetch something from websites etc.?

I will take a look at it. Thanks. I’ve actually been looking for something similar to this course you suggested except it was not a course – just some articles online or some YouTube videos. By far those I found are not actually help me much – only enough to fix specific cases.

understanding file permission will help. The very brief version is that linux only will execute a script if it has the right execution permission (which is very different from windows .exe concept). By default when creating a file a script does not have execute permission

no, .bash_profile is special, this file is read at login, so its more useful for configuration (linux is very configurable). .bashrc is more suitable for putting your aliases.

special files like bash_profile and bash_rc are pre-configured, linux checks if these files exist when certain events are fire (logging in -> bash_profile, launching a terminal -> bashrc). Depending on your linux distro, .bash_profile will probably also read .bashrc

1 Like