Does anybody here know how to run scripts with Termux?

Hi
See this very simple bash script…

#!/bin/bash
echo "Hello World"

With my Linux PC…

1
Make the file executable…
$ chmod +x filename

2
Run the script…
$ ./filename

The reply is…
Hello World

But when I try this with Termux it gives reply…
$ bash: ./filename: Permission denied

I have read some information about this at
https://wiki.termux.com/wiki/Termux-fix-shebang
and
https://wiki.termux.com/wiki/Termux-exec

But I can’t get it to work.

How do I make a “Hello World” script for Termux and how do I make it run?

1 Like

Make sure that the current working directory matches the directory of the file. I guess you moved the file to /sdcard/filename. Termux, by default, starts somewhere like /data/data/com.termux/files/home/termux.

1 Like

Also, in Permissions give it Storage, so then you can put your script anywhere (as allowed by Android):

2 Likes

You can’t make external storage file executable. If your file on external storage either move it to termux home directory or execute like this bash filename

2 Likes

" You can’t make external storage file executable. If your file on external storage either move it to termux home directory or execute like this bash filename"

Thanks shubhamtyagi
:sunglasses:

Yes, I can run the script if I use…
bash filename
or
sh filename

(whether it is executable or not)

“filename” is in “shared” directory not “termux home” directory.

“termux home” directory is higher up the hierarchy than “shared”.
I don’t think that I can put files in “termux home” because it doesn’t show in my file manager.
Maybe I would need a rooted phone to do that.

I suppose that “storage” and “storage/shared” is viewed as “external storage” by Termux.

See the hierarchy…
files → home → storage → shared → “filename”

$ ls
app_textures  code_cache  shared_prefs
app_webview   files
cache         lib
$ cd files
$ ls
home  usr
$ cd home
$ ls
storage
$ cd storage
$ ls
dcim       movies  pictures
downloads  music   shared
$ cd shared
$ ls
Android    Movies         Xiaomi
DCIM       Music          fac_sources
Documents  Pictures       filename
Download   Scripts
MIUI       SoundRecorder
$ bash filename
Hello World
$ sh filename
Hello World
$

1 So start Termux.

2 Then cd to storage/shared.

3 Then run bash filename or sh filename.

Is that the solution?

1 Like

Termux can run script from widget. Just put the script in home/.shortcuts then you can add a widget for it.

My AOSP file manager can access termux home dir via SAF.

2 Likes

Wake up shubhamtyagi
:astonished:

1 Like

Is this working?

If not then copy your file to termux home (or wherever other than external storage).

2 Likes

use cp or mv command

2 Likes

**

Yes, that’s fixed it.
:smiley:

Copied filename from storage/shared to Termux home…

$ cp storage/shared/filename .
$ ls
filename storage

Tried to run it…

$ ./filename
bash: ./filename: Permission denied

Made it executable…

$ chmod +x filename

Ran it OK…

$ ./filename
Hello World

$ cp storage/shared/filename .
$ ls
filename storage
$ ./filename
bash: ./filename: Permission denied
$ chmod +x filename
$ ./filename
Hello World
$

1 Like

Yes, that works for me too.
Thanks

1 Like

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.