1

I am trying to follow this example in the Bash Guide over here where one can run a script just by its name instead of bash scriptname. First I used these three commands to create a bin directory, add its path to PATH, add that to the bash configuration file, and then reloading it:

$ mkdir -p "$HOME/bin"
$ echo 'PATH="$HOME/bin:$PATH"' >> "$HOME/.bashrc"
$ source "$HOME/.bashrc"

Then, I created a simple script that goes like this:

#!/usr/bin/env bash


echo "Hello World"

However, when I try to run it just by its name in the terminal, it gives me the error:

bash: /home/fieldsofgold/bin/mybashscript: Permission denied

Could anyone please help me with this? Thanks.

EDIT: Output for ls -l:

fieldsofgold@fieldsofgold-VirtualBox:~$ ls -l
total 152
-rwxrwxr-x  1 fieldsofgold fieldsofgold 13469 Mar 28 17:17 a.out
drwxrwxr-x  2 fieldsofgold fieldsofgold  4096 Aug 12 06:23 bin
drwxr-xr-x  9 fieldsofgold fieldsofgold  4096 Aug 11 07:55 Desktop
drwxr-xr-x  2 fieldsofgold fieldsofgold  4096 Feb 13 19:15 Documents
drwxr-xr-x  3 fieldsofgold fieldsofgold  4096 Aug 11 10:49 Downloads
-rwxrwxr-x  1 fieldsofgold fieldsofgold  8509 Mar 19 12:10 ex1
-rw-rw-r--  1 fieldsofgold fieldsofgold  1852 Mar 25 19:59 ex1.c
-rw-rw-r--  1 fieldsofgold fieldsofgold  1827 Mar 25 12:56 ex1.c~
-rw-rw-r--  1 fieldsofgold fieldsofgold  1773 Mar 25 12:43 ex2.c~
-rw-rw-r--  1 fieldsofgold fieldsofgold  1834 Mar 25 12:36 ex3.c~
-rw-rw-r--  1 fieldsofgold fieldsofgold  1786 Mar 25 12:44 ex4.c~
-rw-r--r--  1 fieldsofgold fieldsofgold  8980 Feb 13 18:27 examples.desktop
-rwxr-xr-x  1 root         root            33 Aug  9 19:32 helloworld.sh
drwxr-xr-x  2 fieldsofgold fieldsofgold  4096 Feb 13 19:15 Music
-rw-rw-r--  1 fieldsofgold fieldsofgold     0 Aug 11 15:10 mybashscript~
drwxrwxr-x  4 fieldsofgold fieldsofgold  4096 Jul 29 20:30 nltk_data
drwxrwxr-x  8 fieldsofgold fieldsofgold  4096 Jul 28 19:48 numpy
-rw-rw-r--  1 fieldsofgold fieldsofgold  1571 Mar 28 08:13 oddEvenTrans22.c
-rw-rw-r--  1 fieldsofgold fieldsofgold    38 Mar 25 15:44 oddEvenTrans.c~
drwxr-xr-x  5 root         root          4096 Jul 28 11:18 openblas
drwxrwxr-x 14 fieldsofgold fieldsofgold  4096 Jul 28 11:18 OpenBLAS
drwxr-xr-x  2 fieldsofgold fieldsofgold  4096 Mar 26 04:25 Pictures
-rw-rw-r--  1 fieldsofgold fieldsofgold     0 Mar 25 19:16 practice.c
drwxr-xr-x  2 fieldsofgold fieldsofgold  4096 Feb 13 19:15 Public
drwxrwxr-x  2 fieldsofgold fieldsofgold  4096 Mar 30 09:12 python
drwxrwxr-x  2 fieldsofgold fieldsofgold  4096 Mar 29 18:05 scans
drwxr-xr-x  2 fieldsofgold fieldsofgold  4096 Feb 13 19:15 Templates
-rw-rw-r--  1 fieldsofgold fieldsofgold    78 Mar 16 06:47 testfile
-rw-rw-r--  1 fieldsofgold fieldsofgold  4606 Feb 15 04:16 Vagrantfile
drwxr-xr-x  2 fieldsofgold fieldsofgold  4096 Feb 13 19:15 Videos
-rw-rw-r--  1 fieldsofgold fieldsofgold    29 Aug  9 16:50 vitestfile
-rw-rw-r--  1 fieldsofgold fieldsofgold   591 Mar 28 08:10 wr
2
  • Can you try ls -l in the home directory and post the output?
    – Eric Power
    Aug 12, 2015 at 6:45
  • @EricPower Just did in the original post.
    – QPTR
    Aug 12, 2015 at 6:53

2 Answers 2

2

You will need to change the permissions. Notice the -rw-rw-r-- line in the line about mybashscript~. These characters show the permissions level for the file. Right now you have it so that nothing can execute the file, only read or write it.

An easy fix is chmod 775 ./mybashscript. This will change those characters to -rwxrwxr-x. The added x's mean that anyone can also execute the code, and will allow you to run your code.

If you are concerned that anyone can execute your code, which is a good thing if you are a security minded person or on a shared computer etc. You should look at this webpage that talks about what the different file permissions mean. Once you have a decent understanding of how permissions work you can edit them so that only certain users can read, write or run the file.

14
  • 1
    -1 for chmod 777, and sudo chmod where it isn't needed.
    – muru
    Aug 12, 2015 at 7:02
  • I'll edit to explain the sudo chmod, but why is chmod 777 a bad thing? It may not be as clear to beginners, but once you understand the permissions it's far easier to see exactly what you're setting them to.
    – Eric Power
    Aug 12, 2015 at 7:04
  • chmod 777 is a bad thing, because people apply it without knowing what it does. You may understand what it does, but the answer isn't for you, it's for the users of this site.
    – muru
    Aug 12, 2015 at 7:05
  • 1
    Further, not only are you adding execute permissions, you're also adding write permissions to others. Was that needed?
    – muru
    Aug 12, 2015 at 7:07
  • 2
    @EricPower you've got a bit confused about which sets of permissions apply to which users, in octal, the first number is for the file owner, the second for the group, and the 3rd for everyone else. In this example, the scripts are all owned by 'fieldsofgold' as user and group, which is the signed in user so the first and second numbers will apply to this user. So no need for sudo, or root sign ins.
    – Arronical
    Aug 12, 2015 at 9:57
0

You don't seem to have executable permission for that script,

-rw-rw-r--  1 fieldsofgold fieldsofgold     0 Aug 11 15:10 mybashscript

type

sudo chmod a+x mybashscript

then run it with

./mybashscript

It'll run

2
  • Does this answer help you ? I haven't explained the details about "permissions", you can read this blog for nice explaination about it linux.com/learn/tutorials/… Aug 12, 2015 at 8:15
  • Thank you for the answer. It did but Eric Power's answer seemed more comprehensive.
    – QPTR
    Aug 18, 2015 at 18:22

Not the answer you're looking for? Browse other questions tagged .