Score:0

How can I execute my custom scripts without typing bash before?

bv flag

I want to run my custom scripts but every time I need to execute it I have to type bash before the script name. But I want to run it just like we would run other scripts such as ls git sed chmod etc where typing bash prior to the script name is not required.

I haven't found any solution here, but the closest was this Bash Script does nothing

I have created a script named test

#!/bin/bash

echo Hello

But when I type it on terminal it gives no error and prints nothing

user@notebook:~$ test
user@notebook:~$

But it works when I type it with bash

user@notebook:~$ bash test
Hello
user@notebook:~$

I have added the file to ~/.local/bin/, given it the permission chmod a+x test and ~/.local/bin is set in my PATH variable.


My stat /bin/bash is

  File: /bin/bash
  Size: 1396520     Blocks: 2728       IO Block: 4096   regular file
Device: 806h/2054d  Inode: 30408799    Links: 1
Access: (0755/-rwxr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2023-05-07 12:23:17.179593764 +0500
Modify: 2022-01-06 21:23:33.000000000 +0500
Change: 2022-10-08 20:47:19.225627603 +0500
Birth: 2022-10-08 20:47:19.217622335 +0500

My stat /bin is

  File: /bin -> usr/bin
  Size: 7           Blocks: 0          IO Block: 4096   symbolic link
Device: 806h/2054d  Inode: 12          Links: 1
Access: (0777/lrwxrwxrwx)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2023-05-07 12:22:43.299594371 +0500
Modify: 2022-10-08 20:47:18.020812502 +0500
Change: 2022-10-08 20:47:18.020812502 +0500
Birth: 2022-10-08 20:47:18.020812502 +0500

The output of type -a test is:

test is a shell builtin 
test is /home/saad/.local/bin/test 
test is /usr/bin/test 
test is /bin/test 
Artur Meinild avatar
vn flag
Is Bash actually installed in `/bin/bash`? Please include output of `stat /bin/bash` and `stat /bin`. Also, which version of Ubuntu are you using? Thanks.
Saad avatar
bv flag
i used `which bash` and it returned `/usr/bin/bash`. I have corrected that but still there is no change for me. My ubuntu version is `Ubuntu 22.04.2 LTS`. I have added the `stat` output in my question.
hr flag
Do you perhaps have an alias or shell function that is masking the script? What is the output of `type -a myscript`?
Gilles Quenot avatar
tn flag
The prompt says that you create your script in your HOME directory: `user@notebook:~$ bash myscript` What is the output of `readlink -f myscript` ?
Saad avatar
bv flag
@steeldriver I haven't touched my alias since the installation. The output of `type -a myscript` 'is myscript is a shell builtin myscript is /home/saad/.local/bin/myscript myscript is /usr/bin/myscript myscript is /bin/myscript'
Saad avatar
bv flag
@GillesQuénot the output to `readlink -f myscript` is `/home/saad/.local/bin/myscript`
hr flag
So right there `myscript is a shell builtin` (presumably your script isn't actually called `myscript`, but is named the same as one of the [Shell Builtin Commands](https://www.gnu.org/software/bash/manual/html_node/Builtin-Index.html) ?
Artur Meinild avatar
vn flag
Ok call your scriptfile something else, and try again.
Artur Meinild avatar
vn flag
If your scriptfile is called `test` or `echo`, please edit your question accordingly for clarity.
Raffa avatar
jp flag
I guess the script is named after its contents: `echo` ... hence `/bin/echo` and `/usr/bin/echo` the external echo command ... Bash also has a builtin `echo` ... Had you used the actual script-file's name, it would've been a lot easier for you and us :-)
Artur Meinild avatar
vn flag
I'm leaning more towards `echo` now that you mention it!
Saad avatar
bv flag
It was my mistake that I used a placeholder, I have updated the question to match my problem. Thank you guys!
Gilles Quenot avatar
tn flag
`test.sh` would have been sufficient to avoid this pitfalls.
Score:5
vn flag

This gives away what has really happened:

The output of type -a test is:

test is a shell builtin 
test is /home/saad/.local/bin/test
test is /usr/bin/test
test is /bin/test
  1. Your script has the name test, which is the same as a Builtin Command

  2. NEVER name your scripts exactly the same as internal commands - this will have unwanted consequences.

  3. So you simply need to name your scriptfile something else than test (or any other internal command).

  4. If you want to run your own script instead of a standard application (like ls), you should create an alias with the name and the full path of your script, like:

    alias ls='/home/saad/.local/bin/myscript'
    
Important note:

The next time you ask a question like this, please include the correct name of your script, and not a placeholder name. Now that you have included the correct name, it's obvious to many of the users here that it was called test, which is the same as an internal command.

Saad avatar
bv flag
Thank you and the others who commented. It was my mistake that I used a place holder which lengthened the time to answer here. I appreciate you guys!
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.