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
  • 233

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
  • 197,895
  • 55
  • 485
  • 740
Eric Power
  • 391
  • 3
  • 15
  • 1
    -1 for chmod 777, and sudo chmod where it isn't needed. – muru Aug 12 '15 at 07: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 '15 at 07: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 '15 at 07:05
  • 1
    Further, not only are you adding execute permissions, you're also adding write permissions to others. Was that needed? – muru Aug 12 '15 at 07:07
  • Hence why I posted a link to the Ubuntu page that gives a very thorough explanation of permissions, including how chmod works with either 777 or a+x as an argument, and talked about needing to understand permissions so that anyone can make security decisions themselves. – Eric Power Aug 12 '15 at 07:07
  • Ah, but all that is hidden behind a link and not in your answer, which, on the other hand, gives the worst method possible. – muru Aug 12 '15 at 07:08
  • Actually your right, I took that what I was doing on my terminal where my file was already -rw-rw-rw. I will edit for that. – Eric Power Aug 12 '15 at 07:09
  • @EricPower Thanks for that answer. I'll read up more on file permissions to understand why chmod 777 is not a good thing to do. Actually the mybashscript file I was trying to run was inside the bin folder that I created via the commands I mention, but it had the same permissions so this works in that case too. Thanks @muru for the discussion on chmod 777, I am reading on it. – QPTR Aug 12 '15 at 07:13
  • @EricPower Permissions make a bit more sense now after reading through the article a bit. If I do chmod 775 after 777, would the permissions revert? Also, will 755 work too? in giving only the owner permission. – QPTR Aug 12 '15 at 07:57
  • @QPTR Yep, that's why I like using "777" instead of "a+x" as each time you use the numbers, you are setting every bit. To answer the second part, a) try it :) and b) no, 755 will allow everyone to execute, but only root to write to it. You might want 474 which allows everyone to read it, but only you to write / execute it. Depends on how you want to use it. – Eric Power Aug 12 '15 at 08:06
  • Remember that the first number set's the root, the second the owner and the third the group. – Eric Power Aug 12 '15 at 08:09
  • It works! Ah, but is 755 a more safe thing to do since only I, as an owner would have the permission to write, and everyone else could read and execute? – QPTR Aug 12 '15 at 08:30
  • Possibly? It really depends on your setup. 755 means that if you want to edit the file, you have to sign in as root or use sudo. If that works for you then perfect, that should allow others, as long as they are in the right group, to execute it. – Eric Power Aug 12 '15 at 08:43
  • 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 '15 at 09: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

Prashant Chikhalkar
  • 2,411
  • 2
  • 18
  • 25
  • Does this answer help you ? I haven't explained the details about "permissions", you can read this blog for nice explaination about it https://www.linux.com/learn/tutorials/309527-understanding-linux-file-permissions – Prashant Chikhalkar Aug 12 '15 at 08:15
  • Thank you for the answer. It did but Eric Power's answer seemed more comprehensive. – QPTR Aug 18 '15 at 18:22