Monday, April 29, 2024
19
rated 0 times [  19] [ 0]  / answers: 1 / hits: 12116  / 2 Years ago, wed, april 6, 2022, 1:53:48

I was wondering whether I could make my own commands as in if I were to type in 'music' at the command line, my vlc should open up and start playing my playlist. The vlc thing is just an example but the question remains.



Can I make my own commands? If yes, how?


More From » command-line

 Answers
6

Yes - create a file named music and put the following inside it:



#!/bin/bash
echo "Hello world"


Next, type chmod +x music - this makes the file executable. You can now type ./music to run this script.



It's a good idea to type echo $PATH and copy the script into one of those directories. I would suggest creating $HOME/bin and adding that to your PATH via ~/.bash_profile or ~/.profile. This can be done by adding (to one of those files): export PATH="$HOME/bin:$PATH". You will then need to run source .bash_profile (assuming you put that in that file) to reload it.



Another alternative would be to add the command you want to run as a bash alias. You can do this by adding the following to a file named .bash_aliases in your home directory:



alias music='vlc --some-option --foo'


Run source ~/.bash_aliases or logout and login and it should work :-)


[#38764] Thursday, April 7, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rowbris

Total Points: 419
Total Questions: 122
Total Answers: 101

Location: Norway
Member since Mon, May 23, 2022
2 Years ago
;