You are not logged in.

#1 2014-04-27 07:33:59

rabcor
Banned
Registered: 2013-02-09
Posts: 500

Alias not working

I tried to create an alias that won't work correctly.

alias depclean="sudo pacman -Rns $(pacman -Qqtd)"

The command works fine when I run it from the terminal (it asks pacman about any missing dependencies, and then removes them with a confirmation)

But if I use the alias, the command seems to mess up. I can't pinpoint exactly how though, but I suspect the "$(pacman -Qqtd)" part is confusing the alias somehow.

Right now I tried installing awesomewm then uninstalling it and running the command. In terminal I get this:

$ sudo pacman -Rns $(pacman -Qqtd)
checking dependencies...
:: deluge optionally requires python2-mako: needed for web ui

Packages (7): gobject-introspection-1.40.0-1  python2-beaker-1.6.4-2
              python2-mako-0.9.1-2  python2-markupsafe-0.21-1
              libxdg-basedir-1.2.0-2  lua-lgi-0.7.2-1  xcb-util-cursor-0.1.1-1

Total Removed Size:   11.45 MiB

:: Do you want to remove these packages? [Y/n] n

With alias:

$ depclean
error: no targets specified (use -h for help)

If I were to try to point a finger at what happening, alias does run the "$(pacman -Qqtd)" part, but not correctly or at the very least, not every time the command is written.

Last edited by rabcor (2014-04-27 07:40:42)

Offline

#2 2014-04-27 07:37:06

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Alias not working

It needs to be a function: you can't do command susbstitution in an alias.


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#3 2014-04-27 07:44:21

rabcor
Banned
Registered: 2013-02-09
Posts: 500

Re: Alias not working

I see, damn... do you have any idea what I can use instead?

I got this command recommended as something to put in as an alias to begin with :I

Offline

#4 2014-04-27 07:46:19

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Alias not working

This?

depclean(){ sudo pacman -Rns $(pacman -Qqtd) ;}

Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#5 2014-04-27 08:40:15

rabcor
Banned
Registered: 2013-02-09
Posts: 500

Re: Alias not working

what do you mean?

bash: /home/user/.bashrc: line 14: syntax error near unexpected token `('
bash: /home/user/.bashrc: line 14: `alias depclean(){ sudo pacman -Rns $(pacman -Qqtd) ;}'

Offline

#6 2014-04-27 08:51:00

sirdeiu
Member
Registered: 2013-03-11
Posts: 10

Re: Alias not working

rabcor wrote:

what do you mean?

bash: /home/user/.bashrc: line 14: syntax error near unexpected token `('
bash: /home/user/.bashrc: line 14: `alias depclean(){ sudo pacman -Rns $(pacman -Qqtd) ;}'

He said function. place the line in you  .bashrc file just as is. That creates a function named depclean. If you put alias in front of it, bash tries to make an alias.

Offline

#7 2014-04-27 09:01:17

jasonwryan
Anarchist
From: .nz
Registered: 2009-05-09
Posts: 30,426
Website

Re: Alias not working


Arch + dwm   •   Mercurial repos  •   Surfraw

Registered Linux User #482438

Offline

#8 2014-04-27 10:10:25

karol
Archivist
Registered: 2009-05-06
Posts: 25,440

Re: Alias not working

Offline

#9 2014-04-27 11:48:35

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: Alias not working

karol wrote:

So you need to escape the dollar sign or use single quotes to prevent bash from performing the substitution at the time .bashrc gets loaded.

Offline

#10 2014-04-28 05:26:32

derekArch
Member
Registered: 2013-02-02
Posts: 22

Re: Alias not working

You could create a smallscript called depclean with

#!/bin/bash
sudo pacman -Rns $(pacman -Qqtd)

Save into a text file called depclean then chmod +x it. You can then add it to your bin directory if you want to put it on your path

Last edited by derekArch (2014-04-28 05:26:58)

Offline

#11 2014-04-28 06:03:21

Xyne
Forum Fellow
Registered: 2008-08-03
Posts: 6,965
Website

Re: Alias not working

When you use double-quotes in the alias, Bash will expand the subshell $(pacman -Qqtd) to whatever the current output of pacman -Qqtd is when you create the alias. The first time you run the command it will remove those packages. The second time it will try to remove the same packages that you have already removed.

Use single quotes instead to expand the subshell with each invocation (i.e. to create a dynamic command):

alias depclean='sudo pacman -Rns $(pacman -Qqtd)'

To see expansion of the alias, type the alias in a (Bash) shell and press ctrl+alt+e. If the names of the packages do not change when you run the command then you have created a static alias.


My Arch Linux Stuff • Forum Etiquette • Community Ethos - Arch is not for everyone

Offline

Board footer

Powered by FluxBB