How do I make a shortcut bash script in Termux terminal android app?

2

3

I had to resort to using Termux since the developer of Servers Ultimate Pro has not been updating the Node.js package for a long time and the new ES6 features are breaking my scripts. After browsing instructions, I've got latest Node.js installed through apt-get and I can run my script.

However, I have to type:

$node storage/shared/folder_in_internal_android_memory/app.js

If possible could I make a bash script called "bot.sh" with:

#!bin/bash
node storage/shared/folder_in_internal_android_memory/app.js

Then run with:

$bot

I can cp an already created script but I don't know where to place it to allow me to shortcut it.

Nova

Posted 2017-10-05T04:07:45.407

Reputation: 135

Answers

2

How about using aliases?

nano/vi (or whatever you edit with) /data/data/com.termux/files/usr/etc/bash.bashrc

Add for each alias;

alias nodeapp='node storage/shared/folder_in_internal_android_memory/app.js'

anders

Posted 2017-10-05T04:07:45.407

Reputation: 331

I just tested alias s="node storage/shared/folder_in_internal_android_memory/app.js" and rans`. It works but it's not remembered for all sessions. Once I close Termux it forgets the command. Is it possible to set it to permanent remember it for all future sessions? – Nova – 2017-10-06T00:50:23.270

Did you just enter the alias, or did you edit the mentioned file and the alias still didn't persist? – anders – 2017-10-06T01:13:18.933

The node file still exist. It just forgets the alias. – Nova – 2017-10-06T01:18:16.660

That was not what I meant. Did you edit: /data/data/com.termux/files/usr/etc/bash.bashrc ? – anders – 2017-10-06T01:18:58.490

Actually, I'm actually following this https://plus.google.com/+KanatTabaldiev/posts/RKavjA1zaCp now but I can't seem to get VIM to exit when I type :x and enter. I presume I just type s="node storage/shared/folder_in_internal_android_memory/app.js on one line and save.

– Nova – 2017-10-06T01:22:23.773

To save and exit vim use :wq . You need to put alias in front of the s or else it will not work. You might have to restart Termux session. – anders – 2017-10-06T01:27:13.417

It's treating the editor as text. It's not working when I enter :wq<enter key>. I've tried using Volume Up + E to escape but still locked in VIM. – Nova – 2017-10-06T01:42:34.877

Never mind. I've done it. Thanks for your help. I needed to Volume Up + E to trigger ESC since my keyboard doesn't have it and type :wq. – Nova – 2017-10-06T01:46:20.340

5

You need to place the script in ~/.shortcuts/ and install the Termux:Widget app. Then Termux:Widget options appear in your homescreen launcher's widgets list.

Per nico's comment below, to access a typical launcher's widget list, long-press an empty space and tap the option that says "widgets" or "all shortcuts". Then find and long-press the desired Termux shortcut widget and drop it where you want.

Also note that Android lacks /bin and /usr/bin, so it is necessary to change LD_PRELOAD by running pkg install termux-exec and then restarting Termux.

Mark Haferkamp

Posted 2017-10-05T04:07:45.407

Reputation: 431

1

I spent a long time googling to find “the usual place to add widgets to your home screen”, because I thought it was a Termux feature, when in fact it is an Android feature. For those who don’t know it yet, the solution is to press and hold on an empty space on your home screen and then tap Widgets / All shortcuts. I found the solution here: http://www.samsung.com/uk/support/mobile-devices/what-are-widgets-and-how-do-i-add-them-to-my-android-smartphone-or-tablet/

– nico – 2018-05-26T19:06:11.307

@nico Thanks for the feedback! I updated the answer to be clearer (and also updated the /bin and /usr/bin workaround while I was at it). – Mark Haferkamp – 2018-06-30T10:40:50.290

1

Mark's answer got me on the right trail which is probably beyond the scope of what the OP asked for but I guess this might help other looking for some more automated solution:

The Termux:Task (com.termux.tasker) app allows to integrate execution of scripts into custom flows for tasker (and compatible, like Automate) programs.

Note that scripts to be executed need to be put to ~/.termux/tasker/ (folder also needs to be created).

antiplex

Posted 2017-10-05T04:07:45.407

Reputation: 131