# Modify a script to add the executable bit after it runs

**URL:** <https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264>\
**Category:** Shell Programming and Scripting\
**Created:** [September 12, 2025, 12:52pm UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264 "2025-09-12T12:52:28Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![drew77](https://community.unix.com/user_avatar/community.unix.com/drew77/32/23164_2.png) [@drew77](https://community.unix.com/u/drew77)\
**Post date:** [September 12, 2025, 12:52pm UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/1 "2025-09-12T12:52:28Z")

</div>

Can I modify this script so it makes itself be executable ?

I could use help in making my posts more visually appealing when it has code.

Thanks.

```plaintext
#!/bin/bash

# Release 22.04.5 LTS (Jammy Jellyfish) 64-bit

# 

# Much help from https://ubuntu-mate.community/

# 

# Run this periodically to clean up unneeded files

# \*\*\*\* This is in crontab.

clear
echo pain | sudo -S apt -y update
echo really | sudo apt-get dist-upgrade
echo hurts | sudo -S apt-get clean

# autoclean clears out the local repository of retrieved package files

# Local repository of package files are in /var/cache/apt/archives/

echo pain | sudo -S apt autoremove
echo CleanUp.sh was run on > /home/andy/Downloads/Trash_Cleanup$( date '+%m-%d-%Y' ).txt
date +"%I:%M:%S %P %D" >> /home/andy/Downloads/Trash_Cleanup$( date '+%m-%d-%Y' ).txt

```

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [September 12, 2025, 5:16pm UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/2 "2025-09-12T17:16:22Z")

</div>

> [@drew77](#):
>
> ```plaintext
> echo CleanUp.sh was run on > /home/andy/Downloads/Trash_Cleanup$( date '+%m-%d-%Y' ).txt
> date +"%I:%M:%S %P %D" >> /home/andy/Downloads/Trash_Cleanup$( date '+%m-%d-%Y' ).txt
> 
> ```

Certainly you want

```plaintext
{
echo "CleanUp.sh was run on"
date +"%I:%M:%S %P %D"
} > /home/andy/Downloads/Trash_Cleanup$( date '+%m-%d-%Y' ).txt

```

You can run a not executable script by invocation of bash:

```plaintext
/bin/bash /path/to/yourscript ...

```

Otherwise it must be executable, and the kernel inserts the interpreter given in the shebang.

```plaintext
/path/to/yourscript ...

```

Are you talking about how to post code in this forum?  
Wrap it between ````` lines! I did it with your current post.  
You can use the `</>` icon at the top of the editor, either on your selected code, or on a new line and then insert your code.

---

<div class="post-metadata">

**Author:** ![Paul\_Pedant](https://community.unix.com/letter_avatar/paul_pedant/32/6_449c533f586939b6e5f246632a46421f.png) [@Paul\_Pedant](https://community.unix.com/u/Paul_Pedant)\
**Post date:** [September 13, 2025, 7:28am UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/3 "2025-09-13T07:28:54Z")

</div>

In addition to only running the write to your clean-up file once, I would:

(a) Assign the generated filename to a variable at the top of the script, rather than have it hidden down at the end.

(b) Append to it every time. If cron only runs once a day it makes no difference. If it runs multiple times, you probably want to see that: you might even want to test it that way.

(c) I prefer the `%Y-%m-%d` date format in filenames. It makes it much easier when you `ls -l` the directory – having all the 4th of every month sort together gets somewhat muddled. Seeing them in chronological order is easier.

Having your sudo password visible in a script can be a serious security issue.

I tend to put the exact crontab line as a comment in the script, as a reminder. crontab can occasionally get munged accidentally.

---

<div class="post-metadata">

**Author:** ![drew77](https://community.unix.com/user_avatar/community.unix.com/drew77/32/23164_2.png) [@drew77](https://community.unix.com/u/drew77)\
**Post date:** [September 13, 2025, 1:05pm UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/4 "2025-09-13T13:05:35Z")

</div>

You are right about having a password in a script.

I am trying to find ways to eliminate most instances of password prompts.

(I have cognitive impairment….memory loss, confusion)

---

<div class="post-metadata">

**Author:** ![drew77](https://community.unix.com/user_avatar/community.unix.com/drew77/32/23164_2.png) [@drew77](https://community.unix.com/u/drew77)\
**Post date:** [September 14, 2025, 1:20am UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/5 "2025-09-14T01:20:27Z")

</div>

This helps as I rarely reboot my computer.

I want to make sure I have a backup done at least once a day.

```plaintext
#!/bin/sh

# Backup files to maxtor drive before the computer has been suspended

# Put this in /lib/systemd/system-sleep/

case "$1" in
pre)
logger Files being backed up to Maxtor drive.
/home/andy/bin/Backup_Ubuntu_Mate.sh
logger done with backup, going to sleep.
;;
post)
logger waking up from sleep.
;;
esac

```

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [September 17, 2025, 6:35am UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/6 "2025-09-17T06:35:11Z")

</div>

### ChatGPT 4.1 replies to original question:

### 1. **Can a script make itself executable?**

Yes, but only if it is run by a user with permission to do so.  
**How:**  
At the start or end of the script, add:

```bash
chmod +x "$0"

```

This will make the script itself executable if it is not already.

* * *

### 2. **How to post visually appealing code on forums**

- **Use Markdown code blocks:**

- **Example:**

- **For Ubuntu-Mate community:**

- **Add brief comments/section headers to explain logic.**

- **Avoid excessive indentation.**

- **Preview your post before publishing.**

* * *

### 3. **Your script, improved formatting & with self-executable line:**

```bash
#!/bin/bash

# Release 22.04.5 LTS (Jammy Jellyfish) 64-bit
# Much help from https://ubuntu-mate.community/
# Run this periodically to clean up unneeded files
# This is in crontab.

chmod +x "$0" # Makes script executable

clear
echo pain | sudo -S apt -y update
echo really | sudo apt-get dist-upgrade
echo hurts | sudo -S apt-get clean

# Autoclean clears out local repository of retrieved package files
echo pain | sudo -S apt autoremove

# Log file output
echo "CleanUp.sh was run on" > "/home/andy/Downloads/Trash_Cleanup$(date '+%m-%d-%Y').txt"
date +"%I:%M:%S %P %D" >> "/home/andy/Downloads/Trash_Cleanup$(date '+%m-%d-%Y').txt"

```

* * *

**Summary:**

- Add `chmod +x "$0"` to make script executable.
- Use triple backticks and specify language (e.g., `bash`) for best code formatting in posts.
- Add clear comments and structure.

**Skippy Out.**

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [September 17, 2025, 5:12pm UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/7 "2025-09-17T17:12:10Z")

</div>

> [@Neo](#):
>
> `chmod +x "$0"`

Remaining question: how do you execute a script that is not executable?

And: once being executable, does it make sense to make itself executable?

(I hope skippy won’t panic on this.)

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [September 18, 2025, 1:10am UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/8 "2025-09-18T01:10:02Z")

</div>

@MadeInGermany

## ChatGPT 4.1 Replies:

### 1. **How do you execute a script that is _not_ executable?**

- **Call the interpreter explicitly:**

- **This runs the script regardless of file executable bit.**

* * *

### 2. **Once a script is executable, does it make sense for it to make itself executable?**

- **No.**

* * *

**Summary Table**

| Not Executable | Executable |
| --- | --- |
| `bash script.sh` | `./script.sh` |
| `sh script.sh` | |

* * *

**Bottom line:**

- Execute non-executable scripts with `bash script.sh`.
- No real point in making a script make itself executable **every run**.

**Skippy doesn’t panic. Skippy answers.**

---

<div class="post-metadata">

**Author:** ![Paul\_Pedant](https://community.unix.com/letter_avatar/paul_pedant/32/6_449c533f586939b6e5f246632a46421f.png) [@Paul\_Pedant](https://community.unix.com/u/Paul_Pedant)\
**Post date:** [September 18, 2025, 10:46am UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/9 "2025-09-18T10:46:06Z")

</div>

Your script comments that it is executed via `crontab`. I have not verified any of this, but will edit this post later to confirm these notes. I am referring to `man -s 5 crontab` and `command 'cron' from deb cron (3.0pl1-137ubuntu3)`

(1) “Each command is executed by /bin/sh, or by the shell specified in the SHELL variable of the crontab file.”

On my Linux Mint 21.3, I get `lrwxrwxrwx 1 root root 4 Nov 16 2024 /bin/sh -> dash`, which may not be what you expected.

(2) If your cron command is like `/bin/bash -c ‘myScriptName’`, cron will run /bin/sh, which will run /bin/bash, which (I suspect) will ignore the shebang in your script (as it is already in bash) and not require the file to be executable. So setting execute status at all may not be necessary, let alone every time it runs.

(3) It is fairly unclear what “executed by” means here. cron puts a wrapper round your command, which changes to your own user, puts you in your own home directory, and collects any output to stdout and stderr so it can (by default) mail it to you. Whether that uses your shebang is unclear.

---

<div class="post-metadata">

**Author:** ![drew77](https://community.unix.com/user_avatar/community.unix.com/drew77/32/23164_2.png) [@drew77](https://community.unix.com/u/drew77)\
**Post date:** [September 18, 2025, 10:58am UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/10 "2025-09-18T10:58:27Z")

</div>

#!/bin/bash

#Test script to see if script can be made self-executing

chmod +x "$0"  
gxmessage -fg red -font 'sans 20' -timeout 5 "Backup is complete."

andy@7:~/bin$ SelfExecutingScript.sh  
bash: /home/andy/bin/SelfExecutingScript.sh: Permission denied

---

<div class="post-metadata">

**Author:** ![Neo](https://community.unix.com/user_avatar/community.unix.com/neo/32/22838_2.png) [@Neo](https://community.unix.com/u/Neo)\
**Post date:** [September 19, 2025, 11:34pm UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/12 "2025-09-19T23:34:27Z")

</div>

> [@drew77](#):
>
> andy@7:~/bin$ SelfExecutingScript.sh  
> bash: /home/andy/bin/SelfExecutingScript.sh: Permission denied

I think this is because you need to call your script the first time like this:

```bash
bash /home/andy/bin/SelfExecutingScript.sh

```

or

```bash
sh /home/andy/bin/SelfExecutingScript.sh

```

Something like that.

---

<div class="post-metadata">

**Author:** ![cyclist3](https://community.unix.com/letter_avatar/cyclist3/32/6_449c533f586939b6e5f246632a46421f.png) [@cyclist3](https://community.unix.com/u/cyclist3)\
**Post date:** [September 22, 2025, 3:30am UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/13 "2025-09-22T03:30:40Z")

</div>

> [@MadeInGermany](#):
>
> You can run a not executable script by invocation of bash:
> 
> ```plaintext
> /bin/bash /path/to/yourscript ...
> 
> ```
> 
> Otherwise it must be executable, and the kernel inserts the interpreter given in the shebang.

I verified that this works.

On the idea of a script adding the executable bit, what do you think of this?

Have the script create another script that adds the executable bit to the invoking script….

Thanks.

---

<div class="post-metadata">

**Author:** ![Paul\_Pedant](https://community.unix.com/letter_avatar/paul_pedant/32/6_449c533f586939b6e5f246632a46421f.png) [@Paul\_Pedant](https://community.unix.com/u/Paul_Pedant)\
**Post date:** [September 22, 2025, 8:31am UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/14 "2025-09-22T08:31:30Z")

</div>

The flaw seems to be that the original script needs to be executable so that it can run, and thus create the other script. Something of a Catch-22. If it could run, it could change its own permissions.

It can however shoot itself in the foot by removing its own execute permissions. This might be useful, to make a run-once script.

```plaintext
paul: ~ $ cat myFoot
#! /bin/bash

set -x
ls -l "$0"
chmod -x "$0"
ls -l "$0"
chmod +x "$0"
ls -l "$0"
chmod -x "$0"
ls -l "$0"
echo Ran myFoot
paul: ~ $ ls -l ./myFoot
-rw-rw-r-- 1 paul paul 124 Sep 22 09:25 ./myFoot
paul: ~ $ chmod +x ./myFoot
paul: ~ $ ls -l ./myFoot
-rwxrwxr-x 1 paul paul 124 Sep 22 09:25 ./myFoot
paul: ~ $ ./myFoot
+ ls -l ./myFoot
-rwxrwxr-x 1 paul paul 124 Sep 22 09:25 ./myFoot
+ chmod -x ./myFoot
+ ls -l ./myFoot
-rw-rw-r-- 1 paul paul 124 Sep 22 09:25 ./myFoot
+ chmod +x ./myFoot
+ ls -l ./myFoot
-rwxrwxr-x 1 paul paul 124 Sep 22 09:25 ./myFoot
+ chmod -x ./myFoot
+ ls -l ./myFoot
-rw-rw-r-- 1 paul paul 124 Sep 22 09:25 ./myFoot
+ echo Ran myFoot
Ran myFoot
paul: ~ $ ./myFoot
bash: ./myFoot: Permission denied
paul: ~ $ 

```

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [September 22, 2025, 6:32pm UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/15 "2025-09-22T18:32:43Z")

</div>

Haha, why not

```plaintext
rm $0

```

This is art à la Banksy.

---

<div class="post-metadata">

**Author:** ![cyclist3](https://community.unix.com/letter_avatar/cyclist3/32/6_449c533f586939b6e5f246632a46421f.png) [@cyclist3](https://community.unix.com/u/cyclist3)\
**Post date:** [September 23, 2025, 1:51am UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/16 "2025-09-23T01:51:49Z")

</div>

> [@MadeInGermany](#):
>
> `rm $0`

Could you make the script so I can run it ?

Very curious to see what it does and study it.

---

<div class="post-metadata">

**Author:** ![Paul\_Pedant](https://community.unix.com/letter_avatar/paul_pedant/32/6_449c533f586939b6e5f246632a46421f.png) [@Paul\_Pedant](https://community.unix.com/u/Paul_Pedant)\
**Post date:** [September 23, 2025, 9:13am UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/17 "2025-09-23T09:13:13Z")

</div>

I did make the script, and ran it, in my previous post.

The `"$0"` throughout the topic is a bit of a joke. Scripts can be passed arguments -- extra parameters like options and filenames.

The system passes the script its own name first, so `"$0"` would literally be passed as `"./myFoot"`. You might think a script knows what it is called, but actually there are some occasions where the same script can usefully be called with different names.

`rm "$0"` would make any script destroy itself (assuming it had permissions that allowed that to happen).

My command line prompt is `paul: ~ $` so that precedes each command I type. The script is called `myFoot` because it _does_ shoot itself in the foot.

So `cat myFoot` lists the script, starting with the `shebang` and ending with an `echo` command.

The next three commands show the file permissions (with `ls`), make it executable, and show that it became executable.

I then run the script. This test directory is not on my PATH, so I specify my current directory like

`./myFoot`.

The script includes `set -x`, which makes it print every script command before it does it, marking it with a `+` in the first column.

The commands show that the script can set the execute permissions off and on successfully. I carefully leave it off at the end.

Finally, I run the script again from the command line, and it fails because it made itself non-executable.

---

<div class="post-metadata">

**Author:** ![cyclist3](https://community.unix.com/letter_avatar/cyclist3/32/6_449c533f586939b6e5f246632a46421f.png) [@cyclist3](https://community.unix.com/u/cyclist3)\
**Post date:** [September 23, 2025, 12:00pm UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/18 "2025-09-23T12:00:26Z")

</div>

> [@Paul\_Pedant](#):
>
> rm "$0"

> !/bin/bash  
> Release 22.04.5 LTS (Jammy Jellyfish) 64-bit
> 
> Much help from [https://ubuntu-mate.community/](https://ubuntu-mate.community/), [https://community.unix.com](https://community.unix.com),  
> Demo of a self-deleting script
> 
> gxmessage -fg red -font 'sans 20' -timeout 2 "Demo of a self-deleting script"
> 
> rm "$0"

I had to remove the pound signs to get rid of the large fonts.

Thanks Paul. 🙂

---

<div class="post-metadata">

**Author:** ![Paul\_Pedant](https://community.unix.com/letter_avatar/paul_pedant/32/6_449c533f586939b6e5f246632a46421f.png) [@Paul\_Pedant](https://community.unix.com/u/Paul_Pedant)\
**Post date:** [September 23, 2025, 12:12pm UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/19 "2025-09-23T12:12:05Z")

</div>

Quite so. But that snippet does not explain why it works, why it might have an unexpected side effect, or why it might fail if you were not using a GTK-based desktop.

---

<div class="post-metadata">

**Author:** ![cyclist3](https://community.unix.com/letter_avatar/cyclist3/32/6_449c533f586939b6e5f246632a46421f.png) [@cyclist3](https://community.unix.com/u/cyclist3)\
**Post date:** [September 23, 2025, 11:51pm UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/20 "2025-09-23T23:51:25Z")

</div>

Paul. It was only a demo. Just to see it at work.

Are you a recovering perfectionist ? (Innocent humor.)

I am a recovering perfectionist.

Humor: “If you did not see me do it, I did not do it.”

---

<div class="post-metadata">

**Author:** ![Paul\_Pedant](https://community.unix.com/letter_avatar/paul_pedant/32/6_449c533f586939b6e5f246632a46421f.png) [@Paul\_Pedant](https://community.unix.com/u/Paul_Pedant)\
**Post date:** [September 24, 2025, 9:20am UTC](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264/21 "2025-09-24T09:20:58Z")

</div>

I had a very fortunate 50-year career, 1968 to 2018. The last 30 years were as a consultant (very hands-on) in electricity supply systems, including 7 years at UK National Grid. The main vendors for their system were the same guys who blacked out 55 million consumers in the USA in 2003 (culminating in blacking out their own control room). You would be surprised how much stuff needs pumping: water, sewage, aircon, gas and diesel; and things like freezers (domestic, shop and warehouse), cell phone masts, elevators, hospital equipment, subway trains, and EV charging.

So imperfection could mean letting the system energise a 275,000 volt cable that some poor rigger was working on at the top of a transmission tower, or dropping the entire power supply to Heathrow Airport for two days (which actually happened a few months ago). Not on my watch, thank you!

[Next page](https://community.unix.com/t/modify-a-script-to-add-the-executable-bit-after-it-runs/397264.md?page=2)
