Using Symbolic Notation for Permissions
While numeric notation is concise, symbolic notation can be more intuitive. Let's practice using symbolic notation to change file 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.
echo "Hello, World"
is a command that will print "Hello, World" when the script runs.
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
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.
Now, let's add execute permission for the owner:
chmod u+x script.sh
In this command:
u
refers to the user (owner)
+x
adds execute permission
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
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.