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

26,306

Solution 1

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'

Solution 2

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.

Solution 3

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).

Share:
26,306

Related videos on Youtube

Nova
Author by

Nova

Updated on September 18, 2022

Comments

  • Nova
    Nova over 1 year

    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
    Nova over 6 years
    I just tested alias s="node storage/shared/folder_in_internal_android_memory/app.js" and ran s`. 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?
  • anders
    anders over 6 years
    Did you just enter the alias, or did you edit the mentioned file and the alias still didn't persist?
  • Nova
    Nova over 6 years
    The node file still exist. It just forgets the alias.
  • anders
    anders over 6 years
    That was not what I meant. Did you edit: /data/data/com.termux/files/usr/etc/bash.bashrc ?
  • Nova
    Nova over 6 years
    Actually, I'm actually following this 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.
  • anders
    anders over 6 years
    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.
  • Nova
    Nova over 6 years
    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
    Nova over 6 years
    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.
  • nico
    nico almost 6 years
    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: samsung.com/uk/support/mobile-devices/…
  • Mark Haferkamp
    Mark Haferkamp almost 6 years
    @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).
  • Community
    Community over 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
  • DarkDiamond
    DarkDiamond over 2 years
    If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From Review