Find the answer to your Linux question:
Results 1 to 2 of 2
$ gawk -F':' '{ print $1}' /etc/passwd The above command will display all the users. I make an alias for this command, $ alias lsusers="gawk -F':' '{ print $1}' /etc/passwd" ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jan 2013
    Posts
    1

    alias to list all the users


    $ gawk -F':' '{ print $1}' /etc/passwd

    The above command will display all the users.

    I make an alias for this command,

    $ alias lsusers="gawk -F':' '{ print $1}' /etc/passwd"

    The I use the alias to issue the command

    $ lsusers

    It is supposed to do be the same as

    $ gawk -F':' '{ print $1}' /etc/passwd

    But lsusers not just list users, it is the same as
    cat /etc/passwd

    What is wrong with this alias?

    $ alias lsusers="gawk -F':' '{ print $1}' /etc/passwd"

  2. #2
    Trusted Penguin Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    3,376
    Hi and welcome

    The problems are quoting and (lack) of escaping.
    If you call alias after defining your lsusers, you will see this mess:
    Code:
    $ alias
    alias lsusers='gawk -F'\'':'\'' '\''{ print }'\'' /etc/passwd'
    As you can see, single quotes have been added and escaped. Which looks ugly.
    But also the $1 didnt make it into the alias, as it has been already interpreted as empty by the shell.

    So you could escape the $:
    Code:
    $alias lsuers="gawk -F':' '{ print \$1}' /etc/passwd"
    $alias
    alias lsusers='gawk -F'\'':'\'' '\''{ print $1}'\'' /etc/passwd'
    And this actually works.

    But this approach has issues:
    - ugly quoting
    - escapes needed
    - hardcoded /etc/passwd
    - limited to /etc/passwd
    - required gawk, which may not be available


    My suggestion is to use getent.
    getent is part of the libc package, cut is in the core-utils package. Which means they *will* be available.
    And getent will query the same user/group/network/etc backends (be it files or ldap or whatever) that the system itself uses.

    So your alias would look like this:
    Code:
    $alias lsusers='getent passwd | cut -d ":" -f1'
    $alias
    alias lsusers='getent passwd | cut -d ":" -f1'
    Last edited by Irithori; 01-31-2013 at 09:48 AM.
    You must always face the curtain with a bow.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •