Ubuntu – Execute permission while creating shell script

bashpermissionsscripts

Can I provide execute permission to a shell script while creating it for the first time?

I want a line of code which allows me to give execute permission to a file so that I can add that line inside the file itself. Assume that I know the filename beforehand.

Best Answer

Not sure if I understand you correctly, but if you want a script file that sets its own execution permission, you have to include this command:

chmod +x -- "$0"

The shell variable $0 always holds the name of the running script. Note the double quotes and double dash to protect misinterpretation of special file names.

However, to launch that script the first time, you need to call the interpreter with the script file as argument, like this:

bash example.sh