Score:2

How to reload Bash script in ~/bin/script_name after changing it?

us flag

Context

After exporting a script to ~/bin/script_name and running it successfully from the CLI at an arbitrary place with: script_name, I have updated the content of the script. However, this update is not reflected in the behaviour.

Old content:

#!/bin/bash
echo "Old world"

New content:

#!/bin/bash
echo "Hello world"

I saved the file, and reopened it, and it shows the new content.

Expected output:

Hello world

Actual output:

Old world

Question

How can I ensure the renewed content from the ~/bin/script_name is used when I call it from an arbitrary location using: script_name?

Attempts

First I tried closing and re-opening the terminal. Then I tried rebooting, both show the same, unexpected (to me) behaviour.

Additional information

Ubuntu 22.10, and the $PATH content is:

/home/name/miniconda/envs/snncompare/bin:/home/name/miniconda/condabin:/home/name/.local/bin:/home/name/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin
hr flag
What does `type -a script_name` return? The bash shell caches executable *locations* - but not (AFAIK) their contents.
a.t. avatar
us flag
`type -a script_name` returns: `script_name is /home/name/.local/bin/script_name`, and `script_name is /home/name/bin/script_name`.
Paul_Pedant avatar
fr flag
Bash process retains a hash table of locations it has previously searched for via PATH. Any time you move an executable between directories, you should clear that history with the built-in `hash -r`. You can inspect the complete hash table with `hash` alone.
Score:8
vn flag

You have a copy of the script located at /home/name/.local/bin/script_name which you did not update. This variation of the script are found first in the path.

Hence, when you run it from an arbitrary location, you get the old script.

Remove the script from this location:

rm /home/name/.local/bin/script_name

And only have your script in one location. Otherwise, you'll create a mess for yourself.

a.t. avatar
us flag
Thank you, I have confirmed that removing the file from `~/.local/bin/script_name` and closing and re-opening the terminal resolved the issue. I will accept this answer when I can.
Score:1
us flag

A solution was found by (also) copying the file to the ~/.local/bin folder. As I learned from the comment by steeldriver.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.