I want to create a custom command in linux, but I want to use another command within it, in this case I want to create a command that shows me the version of angular, help please.
you can create the example script:
#!/bin/bash
echo "angular version $(ng version)"
then create a symbolic link to the / bin folder or simply copy it example:
ln -s /home/user/tu_scrip.sh /bin/ngv
cp /home/user/tu_scrip.sh /bin/ngv
with this you can already call ngv
as a command
First of all, to send a script and pass it as a value, you need to pass it as command substitution, that is, run a program inside $ () or inside '' for example:
$echo "Hola $(whoami), haz estado aquí desde $(uptime -s)"
Then your script could be like that.
#!/bin/bash
A_VERSION="$(ng version)"
echo "Angular version $A_VERSION"
Understanding that quotation marks preserve line breaks, IFS, and avoid certain expansions.
Now, for you to call from where you want you can perform several actions to get your custom command:
~ / bin
If it is a program that you will only use and you only when you are logged in with your user, you can create a folder in your directory called bin, that is, mkdir ~/bin
.
Inside that folder you create your script, let's call it custom_command , have the program described above and give it execution permissions, that is, chmod +x ~/bin/custom_command
Then add to your PATH environment variable the path of that folder, that is
PATH=$PATH:~/bin
And with this you can already run it in that session, if you want that to happen every time you open the terminal, you can put that same command in your file ~ / .bashrc or ~ / .zshrc (depending on the shell you use). Within that file you put PATH=$PATH:~/bin
and you're done, every time you log in only on your user , you can type that command directly and the console will recognize it, you can even add more custom commands to the folder ~ / bin. If you change the user, you will not be able to use it. This could be useful if you are doing tests and do not want anyone else to see them for the moment.
/ usr / local / bin
If you want that program to be available to all users of that distribution, first you have to see the contents of the variable PATH with echo $PATH
so that you see that the folder "/ usr / local / bin" is added. If this is true (usually it is), then you can ensure that you can create your custom command within that folder, that is, the previous program you put in /usr/local/bin
as, for example, /usr/local/bin/custom_command
, you return to give permissions of execution and, independently of the user with which you register, you can execute it.
Generally, my recommendation is that you have a custom scripts folder, it can be in your $ HOME or in / opt /, and from there you create symbolic links for / usr / local / bin
That is to say. In your HOME folder you create a folder named $ HOME / tools and inside you save the scripts as custom_script , you give them execution permission with chmod + x, then you create a symbolic link from each of those files to / usr / local / bin with ln
. Example:
$chmod +x ~/tools/custom_script
$sudo ln -s ~/tools/custom_script /usr/local/bin/custom_script
Avoid adding or modifying files inside the /bin
folder since it does not correspond to what is established in the hierarchy of the file system, you can see the manual about it with man hier
.
Programs within bin
are only those that run in "single user" mode and are the basic tools to repair the system. That is, they are programs exclusively for basic administrative tasks.
On the other hand, the programs within /usr/local/bin
are for local use of your system, that is, you can add and delete files from there and they do not affect the basic tasks of your distribution.
If you only need it as a personal command, for me the simplest thing and without touching the bin folders, is to add a alias
for the command in your ~/.bashrc
(or the one corresponding to the type of shell you are using) .
Edit .bashrc
:
$ nano ~/.bashrc
Add the alias
to the end of the document:
alias ngv='Angular Version: $(ng version)'
Save and close the file
Reload the configuration
$ source ~/.bashrc
Try the command:
$ ngv