aliases vs scripts

leftyleo

New Member
Joined
Aug 6, 2024
Messages
17
Reaction score
5
Credits
142
Here is a question that i would like to pose for thought and discussion

I have made aliases ( wih simple commands and at least one called a function) -- and i have made scripts with bash and python . both of the latter scripts i have succeeded in making executable and placed in my $PATH so that they will run from any directory as a command


What i would like to know is which you have used ?
Which you prefer ? Does it depend on what the script does , the alias's complexity? etc ..

some of the scripts i have made are simple for converting png to jpg .. mobi to epub .. that sort of thing for now .
 


Hi leftyleo

If I cannot get an alias to do what I want then I make a script.

I keep all my scripts in /opt and access them with .desktop files from /usr/local/share/applications.

Have fun,

Vektor
 
Which you prefer ? Does it depend on what the script does , the alias's complexity? etc ..
Anything that takes multiple lines -> scripts. They are in ~/bin although not binary, since in my distro ~/bin is empty and already in my $PATH.

Aliases are one-liners, whether a function or not, in .bashrc
 
Vektor:
-- my distro (ubuntu) doesnt have that file /usr/local/share/applications
I put them in /usr/local/bin/ or .local/bin/
I am not sure which is best... it is only home so i dont think it really matters

new_vintage:
i-- agree -- i think if it is more than making an often used command easier (i.e. one with lots of switches but you use the same set most of the time) than aliases are the way to go .. at least this is what i have seen so far
--- and the srcipts are more along the lines of , well, an actual program to perform a task

-- thank you both .. at least i know i am on the right "$PATH" ( ok i couldnt resist the bad bad pun )

leftyleo
 
Hey leftyleo

I had to create the folder /usr/local/share/applications.

I like to keep my stuff seperate from the OS stuff.

new_vintage pegged it about aliases being one-liners. I tried at first to do more but could not.

And all commands are actually pointing to "programs", but aliases do not reside in a "file" like scripts do.

Have fun,

Vektor
 
sometimes the syntax of bash keeps you from doing aliases for certain instructions. Also, unfortunately aliases just aren't recognized in certain circumstances: like with cron jobs and vim external commands (even though you can use scripts in both circumstances). You also might perfer to use a script for something longer, as editing it later will make it easier.

Also, adding ".sh" like you tend to do with a script to a command allows you to use names that are already taken.
 
new_vintage pegged it about aliases being one-liners. I tried at first to do more but could not.
BTW, that doesn't mean one command. A trivial example::
Code:
alias x='cd && clear'
Goes home and clears the screen in one keystroke.

You can do conditional stuff on one line, like:
Code:
alias blockid='if test "$EUID" = 0 ; then /sbin/blkid ; else su - -c "/sbin/blkid" ; fi'
This one line alias checks if the user is root and if so runs blkid. If not it asks for the root password then runs blkid.
 
I just don't use aliases. It's only in the last month or two, following some of the threads on this forum, that I've even bothered playing with them and, TBH, I just don't see the point. I think it would be bad practice to use an alias (or a script) with the name of a standard command to do some non-standard action or use some non-standard option.

Having said that, my OS, by default, sets up three aliases for "safeties" that do just that:
Code:
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'
which for many users is a life saver. Personally, I -hate- such things as I'm not in the habit of typing "rm my-important-file" unless I want to , y'know, remove said file. I'll let you know if I'm not sure! And yet I haven't (for over a decade) gone so far as remove the aliases. Go figure.

For script placement, I make some premeditated violations of the FHS. I have four places to keep scripts (and a few private binaries):
  • /boot/bin64 for programs specific to the x86_64 architecture
  • /boot/bin32 for programs specific to the x86 architecture
  • /boot/bin for programs not specific to either architecture
  • ${HOME}/bin for programs specific to the current installation (I have a bunch of completely distinct installations on any given machine even though I typically only use one or two of them)

FWIW, less than 1% of my scripts are one liners ("two liners" if you count the shebang). Several are full blown applications with built in help, argument processing, logging, etc, that run to hundreds of lines.

---

As food for thought, especially for those new to linux: One of the beauties of linux is that it is so "cutomizable". You can pick and choose your desktop environment, your file manager, your browser, etc, etc, etc.

But don't "customize" commands just to save a few keystrokes or to avoid having to remember command options even though aliases and scripts allow you to do just that. By all means, use scripts or aliases to bundle up commonly used combinations of commands. But don't give them the same names as existing commands (like the three I was grousing about above) - that will likely lead to confusion. Instead, dig in and learn to use the standard tools/utilities/commands. You'll be glad you did. After you've gained some (a lot of) experience and are no longer a noob (though you may always think of your self as a noob - I still do), go ahead and twek a few commands with aliases or whatever, if you still think it's a good idea... but chances are you won't need/want to.
 
BTW, that doesn't mean one command. A trivial example::
Code:
alias x='cd && clear'
Goes home and clears the screen in one keystroke.

You can do conditional stuff on one line, like:
Code:
alias blockid='if test "$EUID" = 0 ; then /sbin/blkid ; else su - -c "/sbin/blkid" ; fi'
This one line alias checks if the user is root and if so runs blkid. If not it asks for the root password then runs blkid.
Considering pressing ^L will clear your screen...

Signed,

Matthew Campbell
 
at least i seem to be on the same page -- keeping aliases (relatively) simple
and using scripts for more complicated tasks.

I like the scripts that run and end, not ones that keep a shell instance running
unless they are something real time i suppose , like "top" but that's more of an app than a script

Thank you for the insights
back to trying things ( in dedicated dir's so i dont make a mess of the whole system )
 
Hey leftyleo

"dedicated dir's so i dont make a mess of the whole system" Is exactly why I put stuff where I do.
Keep out of the way of the OS as much as possible.

Keep having fun,

Vektor
 
I use a mixture of aliases, functions and scripts in Bash. I have some aliases set up for common, general commands.
For example grep is aliased to:
grep -Hn —color

Because I most often use grep for searching through various codebases for keywords. -H gives me the filename and -n gives me the line-number. Which is what I need most of the time.

If I need to use any additional parameters, I’ll add them to my grep invocation.
And if I want to omit the -Hn options, I’ll escape the alias and provide whatever options I need to be using.

I have aliases set up for a lot of other commands. I have a bunch of uniquely named aliases set up to control cmus (terminal based music player) using cmus-remote.
I have aliases used to control audio playback (volume/mute, play/pause, stop, next/previous song, shuffle on/off etc), or to add a song/directory to the current playlist/queue, or to add it to the music library.

I’m not going to list all of my aliases here though. But each one has a unique name.

Generally speaking though, what I use depends on how complex my needs are.

For relatively simple, one or two line commands I’ll use aliases. For anything longer or requiring more complex logic, I’ll create a bash function, or a full blown script.

And if my script is complex, with a lot of different options, I might also set up aliases for the various uses of the script.

And I aways escape aliases for any common commands in my functions/scripts. Especially if they are going to be used on multiple PCs, or distributed to other users. That way, my scripts/functions can avoid accidentally using any aliases that other users have set up for common commands, which could adversely affect their output.

So I use aliases, functions and scripts to improve my time spent in the terminal. And my bash based functions/scripts make liberal use of escaping aliases for common commands, to ensure that they will always run exactly as intended!

Aliases and functions are all in my .bashrc and any personal scripts are in my personal bin directory (~/bin), which also contains scripts in other scripting languages Python, Perl, Ruby etc. plus any binary applications I’ve created using compiled languages like C, C++, rust, go, Java, Haskell etc.
 
Last edited:
i can see this is going to take lots of practice.
and not being afraid to make mistakes and do research for the solutions
 

Staff online

Members online


Top