What's new

Tutorial Termux Guide Update

Sore-Scythe

Honorary Poster
Joined
Jun 9, 2017
Posts
204
Reaction
309
Points
144
Termux Guide Update


softbrick phone ko guys kaya gawa muna ako ng magulong tut para sa inyo. Practice ko nadin tong BBCode. start na agad..

Termux CMDLine Basic
Variables
first of all there are 2 types of variables, the shell variables which are set by the shell and is required for the shell to function properly. The other type is the normal variables or the user-defined variables.

System variables are set at runtime either by the system or by scripts set to run when you open the terminal(termux app).
this system variables include but are not limited to, HOME, PS1, PATH, USER and many more. Now, say you want to see what the value of a variable is, how would you go about doing so? simple. Just run
Bash:
#to see the what's inside the HOME variable run
echo $HOME
#to see the contents of PATH do
echo $PATH

User-defined variables are the variables that you personally set. This can be done with or without the export command
for example:
Bash:
# Let's define a variable named Sore_Scythe and set its value to Pogi
export Sore_Scythe=Pogi
echo $Sore_Scythe
# It works just as well without the export command
Sore_Scythe=Handsome
echo $Sore_Scythe

Now how would we go about deleting this variable? just as easy.
Bash:
# to delete the variable completely
unset Sore_Scythe

# if you just want to erase the content of the variable
export Sore_Scythe=
# or
Sore_Scythe=

pretty simple right? but why would you need to know these stuff?
well you'd wanna know this if you like to mess around with the terminal or other termux users or maybe even linux users.

but before that let's see what some of these system variables are used for.

PS1 contains the template for the command input/prompt. basically, you type the commands after this variable its that thingy you always see
Bash:
u0_a187@localhost:/home$
# [u0_a187] is different for everyone and is based on the termux apps position in the app hierarchy(can be same though)
# [@] is just a separator and can be changed to almost anything.
# [localhost] is the hostname of your device
# [:] is just another seperator
# [$] just to signify the end and where the commands you type appear
you can make your own prompt on these sites
You do not have permission to view the full content of this post. Log in or register now.
You do not have permission to view the full content of this post. Log in or register now.


the PATH variable is a list of paths divided by ":", these paths usually contain the commands that you are using, this is also where you add a path to the folder where you store your scripts. The normal content of the PATH variable in termux is:
Bash:
# if i'm not wrong its
PATH="/data/data/com.termux/files/usr/bin/:/data/data/com.termux/files/usr/local/bin/"
Say you have a folder that contains different scripts, this can be in various extensions like py, sh, pl, rb.
You can run it without going inside the folder as long as you append its path to the HOME variable. then, add execute permission to the script files. and finally, make sure you installed the interpreter and set the correct shebang inside the script.

Bash:
# to add the bin folder which contains the scripts to the PATH variable is easy.
# just do this
export PATH="$PATH:$HOME/bin"

# this sets the path to the contents of the original then just sticks ":$HOME/bin" at the end.
# Btw HOME is another system variable and it usually has "/data/data/com.termux/files/home" inside it
# so "$HOME/bin" becomes "/data/data/com.termux/files/home/bin".
# now see the contents of PATH using the previous command somewhere above.

so how can we use this to mess around with someone?
well for example you could delete the contents of the PATH variable and see what happens.
you can delete the variable completely or just empty out the contents(commands above!!)
if you did it, try running other commands and see if you can get anything to work...

well, how'd it go? pissed yourself? if you can't fix it just exit the terminal(termux) and open it again.

that is one way to mess around with newbies but not as fun as deleting the contents of their storage(not gonna teach you, it's quite easy if you read the first mess of a guide I wrote)

btw if you wanna see all variables you have just run
Code:
printenv

Special Files & Folders
So has anyone here seen the .bashrc file? or maybe the motd file?
what are they for? welp the motd file is quite obvious if you managed to open it.

Let's talk about the .bashrc file...

it contains commands that run when you start the terminal. you can also define functions and aliases but that is usually done in the .bash_aliases file to avoid a messy bashrc.

to add something to the .bashrc file is simple as editing it with a text editor, just mind your syntax and whatnot because it's just a script.

for example to print a good reminder everytime you open the terminal just edit the .bashrc file with your favorite text editor and add a simple command.
Bash:
# to echo something every start
# first open .bashrc with your text editor of choice(mine is micro)
micro .bashrc
# if it's empty you're in the wrong directory(not HOME)
micro $HOME/.bashrc

# add these lines anywhere if you know what you're doing, otherwise just add it at the end.
echo 'U29yZS1TY3l0aGUgaXMgc28gaGFuZHNvbWUhISEK'|base64 -d

# save then restart the termux app or just start a new session.

this is just a very simple example so if you wanna do more look up bashrc scripts on google or github

the motd file is just a text file and it contains the greeting you get everytime you open termux.

very small list of files
run at start
[ul].bashrc, .bash_aliases, .bash_profile[/ul]
run at logout/exit
[ul].bash_logout[/ul]

marami pa yan guys explore lang kayo and try nyo gamitin ang fd paghanap ng files.


Basic Bash Scripting and Command Chaining
Command Chaining
for those to lazy to type each command consecutively you can just chain them and be done with it.
just add a semicolon(;) after each command and type the next one, this simple chain can last for as long as you can type.
Bash:
echo "Sore-Scythe"; echo "is"; echo "so" && echo "effing"; echo 'handsome'

but what if we want the first command to pass it's output to the second one?
we do it with a pipe(|)
Bash:
echo "Sore-Scythe is so effing ugly"|sed 's\ugly\handsome\'
# you can use pipe with a lot of commands but not all of them so experiment or search.

# you can also pipe the cat commmand which is an abbreviation for "read that file"
cat .bashrc|sed -E 's/[a-zA-Z]+/PANGET/'

for those that want the chain to stop if a command fails and those that need a command to run only when the previous command fails.
Bash:
# to stop the chain if the cat command fails use &&
cat secret_born_bookmarks.txt && echo "sadly there is none"

# to run a command when the previous one fails use double ***** (||)
cat secret_born_bookmarks.txt || echo "sadly there is no such file"

# that's about it I think

now for those that need the output of a command to be treated as an argument, use a subshell.
a subshell is spawned when you put commands inside a "$()"

Bash:
# for example
echo "sdcard files $(echo 'ZGVsZXRlZAo='|base64 -d)"s

# the shell stops when it encounter a $()
# it runs it then swap the $() with its output
# then continues

echo "I am in the main shell" "$(echo "I'm in the subshell")"

Basic scripting

i'll mostly talk about loops here.

For loop
Bash:
# this is used to loop over a list or an array. that means it will run command/s on each item in the list/array
# keep in mind that the list can come from anywhere be that an output of ls
# or the lines in a file

# dumb looping - useful when you forgot the technique to use
for count in 1 2 3 4 5
do
	echo "first command, " $(expr $count + $count)
	echo "second command, $(expr $count * $count)"
done

# loop from n to n
for count in {1..200}
do
	echo "number is $count with $(expr 200 - $count) left to go"
done

# the first line defines count as the name for our main variable
# its value changes for each iteration. this loop continues for each
# item in the list which is "1 2 3 4 5" in the first example
# and numbers 1  to 200 in the second.

# for loop is to avoid repeatedly running commands like
number=1;echo "first command, $(expr $number + $number)";echo "second command, $(expr $number \* $number)"
number=2;echo "first command, $(expr $number + $number)";echo "second command, $(expr $number \* $number)"
number=3;echo "first command, $(expr $number + $number)";echo "second command, $(expr $number \* $number)"
number=4;echo "first command, $(expr $number + $number)";echo "second command, $(expr $number \* $number)"
number=5;echo "first command, $(expr $number + $number)";echo "second command, $(expr $number \* $number)"

will continue writing later...
comment down what you think i should write about and i'll try to do so....
 
Last edited:
Salamat sa tutorial and tip another ideas po ulit nadagdag sa braincells ko. ito kasi yung inaaral ko pa last month about bash scripting command and code, hirap na hirap ako dito sa part na to hoping mas mag improved ko pa sarali ko pag dating dito, may kailangan kasi ako paganahin na file na via bash script ko siya ma iinstall at next is ma encrypt yung command and code para iwasan mapakialamanan yung script, salamat dito ts +1 ka sakin 😘
 

Similar threads

Back
Top