User avatar
Protheuz
Posts: 80
Joined: Sat May 11, 2013 5:24 pm
Location: In my chair, bed or sofa.

Shortcuts in LXTerminal!

Wed Nov 27, 2013 7:56 pm

Hellolo! I'm sure some of you guys(and girls, ofc) have heard about the alias command in LXTerminal. It works, but it isn't permanent. Well, this is a tutorial on how to make permanent(unless you remove them) aliases, or in an noob way of saying it, shortcuts, in LXTerminal!
First, start off by using nano to edit .bash_aliases. Be sure your in '/home/pi'!

Code: Select all

sudo nano .bash_aliases
Once your in, you can add a shortcut by simply making a new line, and typing this:

Code: Select all

alias shortcut='command'
Replacing "command" with the command you want to make a shortcut for, and replace "shortcut" with the name of your soon created shortcut..
So, for example, if I want to make a shortcut for "sudo apt-get install" with the name "aginstall", it'll look like this:

Code: Select all

alias aginstall='sudo apt-get install'
When your done adding shortcuts, press CTRL + X, then Y, then ENTER and poof, your done!
Now, do

Code: Select all

sudo reboot
And start using your shortcuts!

Kind regards, Kevin
Wiizard/Protheuz, just call me Kev.

e-raser
Posts: 71
Joined: Sat Apr 04, 2015 1:30 pm

Re: Shortcuts in LXTerminal!

Sun Aug 21, 2016 3:48 pm

Unfortunately it does not work (running on Raspbian = Debian Jessie). Whenever a new SSH session is opened, there is no alias anymore. So it is NOT PERMANENT :( Any ideas?!?

I have:

Code: Select all

-rw-r--r--  1 pi    pi          89 Aug 21 17:30 .bash_aliases
-rw-------  1 pi    pi         15K Aug 21 17:37 .bash_history
-rw-r--r--  1 pi    pi          54 Aug 21 17:18 .bashrc
.bashrc contains:

Code: Select all

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
.bash_aliases contains:

Code: Select all

alias ll='ls -lha --color=auto'
alias sudo='sudo '
alias ..='cd ..'
alias ...='cd ../..'
Command "source ~/.bashrc" is successfully. --> Closing session --> Open new session --> "alias" gives NOTHING (empty).

:cry: :cry: :cry:
1x Nextcloud & Pi-hole & ... on Raspbian @ Pi (4 4 GB)
1x Kodi media center on LibreELEC @ Pi 3 B+

W. H. Heydt
Posts: 12365
Joined: Fri Mar 09, 2012 7:36 pm
Location: Vallejo, CA (US)

Re: Shortcuts in LXTerminal!

Sun Aug 21, 2016 4:14 pm

Repeat use aliases should go into the shell inilialization file which will be something like .profile or .csh, depending on the preferred shell.

Note that aliases have nothing to do with LXterminal. They are functions of the command interpreter, called a "shell". There are a whole bunch of shells,of which not only can you select one for a default (Raspbian is setup to default to bash, but that can be changed), but you can also change what shell you are running "on the fly" by starting a shell from the command prompt. The key thing here is that the shell is not built into the OS, the way the "command prompt" or "DOS box" (for us older folks) is in Windows, though even there, there are ways to change it.

The original unix shell was "sh". The Raspbian default is "bash". There are also "csh" (the C shell, developed at UC Berkeley), ksh (Korn shell, named after its author) and the Bourne shell. Bash, by the way, is a further development of the Bourne shell, as it is an abbreviation of "Bourne Again shell". There are, undoubtedly more, but those come to mind without looking anything up.

e-raser
Posts: 71
Joined: Sat Apr 04, 2015 1:30 pm

Re: Shortcuts in LXTerminal!

Sun Aug 21, 2016 4:20 pm

Well... thanks for the nice history lession :lol:

But back to topic cause I don´t see a solution yet... :cry:
1x Nextcloud & Pi-hole & ... on Raspbian @ Pi (4 4 GB)
1x Kodi media center on LibreELEC @ Pi 3 B+

User avatar
rpdom
Posts: 16984
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Shortcuts in LXTerminal!

Sun Aug 21, 2016 4:49 pm

e-raser wrote:Unfortunately it does not work (running on Raspbian = Debian Jessie). Whenever a new SSH session is opened, there is no alias anymore. So it is NOT PERMANENT
Ok, I just tested this.

I created .bash_aliases with this crude method

Code: Select all

pi@raspi8:~ $ echo "alias ll='ls -lha --color=auto'" >~/.bash_aliases
Then I log out and ssh back in again and check the aliases

Code: Select all

pi@raspi8:~ $ logout
Connection to raspi8 closed.
rpdom@Precision-7510:~$ ssh raspi8

The programs included with the Debian GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sun Aug 21 17:45:51 2016 from laptop
pi@raspi8:~ $ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias ll='ls -lha --color=auto'
alias ls='ls --color=auto'
pi@raspi8:~ $ 
Looks ok to me.
That's on a recently built Jessie Lite system. I haven't made any major changes to it yet.

User avatar
Paeryn
Posts: 2943
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Shortcuts in LXTerminal!

Sun Aug 21, 2016 4:58 pm

e-raser wrote:Unfortunately it does not work (running on Raspbian = Debian Jessie). Whenever a new SSH session is opened, there is no alias anymore. So it is NOT PERMANENT :( Any ideas?!?

I have:

Code: Select all

-rw-r--r--  1 pi    pi          89 Aug 21 17:30 .bash_aliases
-rw-------  1 pi    pi         15K Aug 21 17:37 .bash_history
-rw-r--r--  1 pi    pi          54 Aug 21 17:18 .bashrc
.bashrc contains:

Code: Select all

if [ -f ~/.bash_aliases ]; then
. ~/.bash_aliases
fi
You seem to have an unusually small .bashrc, have you also changed your .profile so that it doesn't source .bashrc? If so then .bashrc won't be processed (and hence neither will .bash_aliases) when bash is started as a login shell (i.e. when you ssh in).
She who travels light — forgot something.

User avatar
rpdom
Posts: 16984
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Shortcuts in LXTerminal!

Sun Aug 21, 2016 5:02 pm

Paeryn wrote: You seem to have an unusually small .bashrc, have you also changed your .profile so that it doesn't source .bashrc? If so then .bashrc won't be processed (and hence neither will .bash_aliases) when bash is started as a login shell (i.e. when you ssh in).
Nice catch!

I assumed that was just part of the .bashrc file, not the whole file.

The default .bashrc file can be copied from /etc/skel/.bashrc

User avatar
scruss
Posts: 3143
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
Contact: Website

Re: Shortcuts in LXTerminal!

Sun Aug 21, 2016 5:30 pm

Protheuz wrote:First, start off by using nano to edit .bash_aliases.
Aliases are neat and all, and I wouldn't be without the ones I've had for years, but Bash developers note in the manual:
For almost every purpose, aliases are superseded by shell functions.
While functions don't have the convenient .bash_aliases file, they do allow much more than the simple command substitution that an alias allows.
Protheuz wrote:

Code: Select all

sudo nano .bash_aliases
You don't need to use sudo to edit your own files. sudo allows you to do far more than a user typically needs to do.
Protheuz wrote:Now, do

Code: Select all

sudo reboot
And start using your shortcuts!
You don't need to reboot after such a simple change. Linux isn't Windows. This will load your aliases in the current and all subsequent shells:

Code: Select all

. ~/.bash_aliases
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

e-raser
Posts: 71
Joined: Sat Apr 04, 2015 1:30 pm

Re: Shortcuts in LXTerminal!

Sun Aug 21, 2016 5:56 pm

rpdom wrote:
Paeryn wrote: You seem to have an unusually small .bashrc, have you also changed your .profile so that it doesn't source .bashrc? If so then .bashrc won't be processed (and hence neither will .bash_aliases) when bash is started as a login shell (i.e. when you ssh in).
Nice catch!

I assumed that was just part of the .bashrc file, not the whole file.

The default .bashrc file can be copied from /etc/skel/.bashrc
I already thought "this would be it" --> copied .bashrc from above listed directory, (changed ownership of file to user pi) and did a "source ~/.bashrc" again. Logged off. Started new SSH session --> "alias" --> No result.

:cry:
1x Nextcloud & Pi-hole & ... on Raspbian @ Pi (4 4 GB)
1x Kodi media center on LibreELEC @ Pi 3 B+

e-raser
Posts: 71
Joined: Sat Apr 04, 2015 1:30 pm

Re: Shortcuts in LXTerminal!

Sun Aug 21, 2016 6:00 pm

Paeryn wrote:You seem to have an unusually small .bashrc, have you also changed your .profile so that it doesn't source .bashrc? If so then .bashrc won't be processed (and hence neither will .bash_aliases) when bash is started as a login shell (i.e. when you ssh in).
To be honest: I know nothing about .profile file. I think (but can´t swear on it) i never touched it. Search for .profile gives me:

Code: Select all

pi@Raspbian:~$ sudo find / -name ".profile"
/etc/skel/.profile
/root/.profile
--> So... there is no .profile for user pi (I guess it should be in /home/pi/) :?: :!: :?:
1x Nextcloud & Pi-hole & ... on Raspbian @ Pi (4 4 GB)
1x Kodi media center on LibreELEC @ Pi 3 B+

User avatar
rpdom
Posts: 16984
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Shortcuts in LXTerminal!

Sun Aug 21, 2016 6:02 pm

e-raser wrote:--> So... there is no .profile for user pi (I guess it should be in /home/pi/)
That file can also be copied from /etc/skel/.profile

It is required for your .bashrc to work

e-raser
Posts: 71
Joined: Sat Apr 04, 2015 1:30 pm

Re: Shortcuts in LXTerminal!

Sun Aug 21, 2016 6:08 pm

rpdom wrote:
e-raser wrote:--> So... there is no .profile for user pi (I guess it should be in /home/pi/)
That file can also be copied from /etc/skel/.profile

It is required for your .bashrc to work
Yes, I already did so.

==> Finally works! The missing .profile file was the root of all evil... for everyone else reading this in the future:

shell login --> .profile --> .bashrc --> .bash_aliases

Thanks guys, I really love this forum and all helping users :!:
1x Nextcloud & Pi-hole & ... on Raspbian @ Pi (4 4 GB)
1x Kodi media center on LibreELEC @ Pi 3 B+

User avatar
Paeryn
Posts: 2943
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Shortcuts in LXTerminal!

Sun Aug 21, 2016 6:15 pm

e-raser wrote:
Paeryn wrote:You seem to have an unusually small .bashrc, have you also changed your .profile so that it doesn't source .bashrc? If so then .bashrc won't be processed (and hence neither will .bash_aliases) when bash is started as a login shell (i.e. when you ssh in).
To be honest: I know nothing about .profile file. I think (but can´t swear on it) i never touched it. Search for .profile gives me:

Code: Select all

pi@Raspbian:~$ sudo find / -name ".profile"
/etc/skel/.profile
/root/.profile
--> So... there is no .profile for user pi (I guess it should be in /home/pi/) :?: :!: :?:
For a login shell bash will first read the system /etc/profile, then it will try reading ~/.bash_profile, if it doesn't exist it tries ~/.bash_login, and if that doesn't exists it tries ~/.profile

Only for non-login shells will bash itself try reading ~/.bashrc.

By default on Raspbian ~/.profile will look to see if bash is the shell and if ~/.bashrc exists, if so it will source it. Copy the version from /etc/skel/.profile to your home directory.
She who travels light — forgot something.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Shortcuts in LXTerminal!

Sun Aug 21, 2016 8:18 pm

scruss wrote: This will load your aliases in the current and all subsequent shells:

Code: Select all

. ~/.bash_aliases
One problem with using this is that it won't remove anything that you've deleted. So if you have two aliases, bobby and jimmy and you remove jimmy and then source the .bash_aliases file, jimmy will still be there.

Rather than sourcing the file, perhaps

exec bash

would be better. It would have the added benefit of any behaviour in bash being the same as what would happen the next time you login, so you'd notice the lack of needed stuff in the .profile file right away.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Shortcuts in LXTerminal!

Sun Aug 21, 2016 8:27 pm

e-raser wrote:shell login --> .profile --> .bashrc --> .bash_aliases
It can get confusing, it can help to put some echo text bits in each file:

echo "I am running the .profile file"
echo "I am running .bashrc file"
echo "I am running the .bash_aliases file"

Then when you login with an interactive shell or not or ssh or whatever, you see what it is running. I've noticed though that sftp doesn't like echo stuff in .profile sometimes, I don't know what the deal is there.

User avatar
Paeryn
Posts: 2943
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Shortcuts in LXTerminal!

Sun Aug 21, 2016 8:57 pm

stderr wrote:
scruss wrote: This will load your aliases in the current and all subsequent shells:

Code: Select all

. ~/.bash_aliases
One problem with using this is that it won't remove anything that you've deleted. So if you have two aliases, bobby and jimmy and you remove jimmy and then source the .bash_aliases file, jimmy will still be there.
If you want to remove an alias from the current shell just use unalias

Code: Select all

pi@rpi3:~ $ alias jimmy='echo Boo'
pi@rpi3:~ $ jimmy
Boo
pi@rpi3:~ $ unalias jimmy
pi@rpi3:~ $ jimmy
-bash: jimmy: command not found
pi@rpi3:~ $
stderr wrote:Rather than sourcing the file, perhaps

exec bash

would be better. It would have the added benefit of any behaviour in bash being the same as what would happen the next time you login, so you'd notice the lack of needed stuff in the .profile file right away.
Except using exec to re-run bash like that will result in bash running as a non-login shell and .profile isn't read then (although .bashrc is). Also .bash_logout won't be run when you subsequently log out. To get the same behaviour as logging in you'd have to do it either with

Code: Select all

exec bash -l
or

Code: Select all

exec -l bash
She who travels light — forgot something.

User avatar
CarlRJ
Posts: 598
Joined: Thu Feb 20, 2014 4:00 am
Location: San Diego, California

Re: Shortcuts in LXTerminal!

Mon Aug 22, 2016 1:52 am

Paeryn wrote:Except using exec to re-run bash like that will result in bash running as a non-login shell and .profile isn't read then (although .bashrc is). Also .bash_logout won't be run when you subsequently log out. To get the same behaviour as logging in you'd have to do it either with

Code: Select all

exec bash -l
or

Code: Select all

exec -l bash
Completely restarting bash with exec is considerable overkill (as was the original suggestion of rebooting - both are in fly/sledgehammer territory). You'll lose all your context - command history, current directory, etc., for no reason. If you simply want some no-longer-wanted aliases to go away, just do "unalias -a" then ". ~/.bash_aliases".

Also, folks in this thread are cargo-culting the ~/.bash_aliases file somewhat. It's only getting read because of the ". ~/.bash_aliases" line in your ~/.bashrc (i.e. you could just as easily change the filename to shiny_happy_aliases in both places and have the same effect, bash doesn't do anything special to/with/because of that filename). Try doing "man bash" and read the section on invocation; all will be explained:
(upon logging in, bash) first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
Personally, I have a ~/.bash_profile that sets up permanent/inherited things, like environment variables (and does "set -o ignoreeof", so ctrl-D will get me out of subshells, but not accidentally drop me out of login shells), and then it sources ~/.bashrc, which primarily sets up shell functions (not aliases) for interactive use, like these:

Code: Select all

function dv () { dirs -v "$@"; }
function hp () { history 22; }
function j  () { jobs "$@"; }
function lf () { ls -p --color=auto "$@"; }
function ll () { ls -l --color=auto "$@"; }
function lr () { ll -rt "$@"; }
function pd () { pushd "$@"; }
function z  () { suspend; }
Shell functions are a much more powerful and flexible way to handle interactive shorthand for commands than what aliases provide (aliases are pretty much obsolete).

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Shortcuts in LXTerminal!

Mon Aug 22, 2016 4:24 am

Paeryn wrote:Except using exec to re-run bash like that will result in bash running as a non-login shell and .profile isn't read then (although .bashrc is). Also .bash_logout won't be run when you subsequently log out. To get the same behaviour as logging in you'd have to do it either with

Code: Select all

exec bash -l
My comment was in response to the

. ~/.bash_aliases

idea which would not deal with the unalias issue. I envisioned someone programming in bash and wanting to get things respun up for testing. Trying to remember the needed commands to update just the necessary bits, what if it's a function that needs gone, what if it's been exported?, seems like more effort in that case than needed. Good point though that exec bash won't cause .profile to run in any case since it won't be a login since it's not logging in. So I'll amend to exec bash -l
CarlRJ wrote:Completely restarting bash with exec is considerable overkill (as was the original suggestion of rebooting - both are in fly/sledgehammer territory). You'll lose all your context - command history, current directory, etc., for no reason. If you simply want some no-longer-wanted aliases to go
That was just an example that broke the source suggestion, restarting bash fixes any number of changes you might've made. I also don't think it's anything like rebooting, I'm going to be testing a bash configuration in a dedicated tab, likely a dedicated shell in byobu in a tab in terminal. Long lasting instances don't need to be restarted often, but a shell that is being updated to test a configuration seems to me ripe for a re go. Of course everyone is free to disagree and argue about it, but I still like exec bash.

Return to “General discussion”