Using Symbolic Notation for Permissions
While numeric notation is concise, symbolic notation can be more intuitive, especially when you only want to change a single permission. Symbolic notation uses letters to represent the user, group, and others, and operators to add or remove permissions.
First, let's create a new script file with some content:
cd ~/project
echo '#!/bin/bash\necho "Hello, World"' > script.sh
This command does two things:
- It creates a new file named
script.sh
. The .sh
extension is commonly used for shell scripts. shell scripts are executable files that contain a series of commands that are executed in sequence.
- It writes two lines into this file:
#!/bin/bash
(called a shebang) tells the system this is a bash script. The shebang line specifies the interpreter that should be used to execute the script. In this case, it's /bin/bash
, which is the path to the Bash interpreter.
echo "Hello, World"
is a command that will print "Hello, World" when the script runs. The echo
command simply displays the text that follows it.
\n
is a newline character, ensuring the commands are on separate lines in the file.
Now, let's check its initial permissions:
ls -l script.sh
You should see something like:
-rw-rw-r-- 1 labex labex 32 Jul 29 16:30 script.sh
As you can see, initially, the script only has read and write permissions for the owner and group, and read permission for others. It doesn't have execute permission, which is required to run it as a program.
Let's try to run the script:
./script.sh
You should see a "Permission denied" error because the script doesn't have execute permissions yet. The ./
part tells the shell to execute the script located in the current directory.
Now, let's add execute permission for the owner using symbolic notation:
chmod u+x script.sh
In this command:
u
refers to the user (owner). Other options are g
for group, o
for others, and a
for all (user, group, and others).
+x
adds execute permission. The +
symbol adds a permission, while the -
symbol removes a permission.
So, u+x
means "add execute permission for the owner."
Let's verify the change:
ls -l script.sh
You should now see:
-rwxrw-r-- 1 labex labex 32 Jul 29 16:30 script.sh
The owner now has rwx
(read, write, and execute) permissions.
Now, let's try running the script again:
./script.sh
This time, you should see the output: "Hello, World"
This example clearly demonstrates why we need to add execute permissions to scripts, and the difference before and after adding these permissions. Symbolic notation makes it easy to modify specific permissions without having to recalculate the entire numeric representation.