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
QPTR
  • 253

2 Answers2

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.

muru
  • 207,228
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

Prashant Chikhalkar
  • 2,451
  • 2
  • 19
  • 25