It is currently Tue, 19 Jun 2018 20:59:09 GMT

Author Message
 variable expansion on the bash command line


        I'm using bash 2.05b.  I want to create a few variables that I can
expand while I am typing on the command line.  For example, I would
like to create

            $SUB="perl -pi -e 's/"

and, by typing $SUB and tab on the command line, have $SUB replaced by its
text, so that the only text appearing after the prompt is

            perl -pi -e 's/

I've read through the bash manual on shell expansions but haven't figured out
how to do what I want.  Can you help me?

Thank you,

 Sun, 03 Feb 2008 06:04:40 GMT   
 variable expansion on the bash command line

I meant

        SUB="perl -pi -e 's/"



 Sun, 03 Feb 2008 06:26:52 GMT   
 variable expansion on the bash command line
On 2005-08-16, John Bullock wrote:

    Use aliases or (preferably) functions.

    Chris F.A. Johnson                     <>
    Shell Scripting Recipes: A Problem-Solution Approach, 2005, Apress

 Sun, 03 Feb 2008 06:55:45 GMT   
 variable expansion on the bash command line

These won't quite do what I want -- at least, in my own experience, they
don't.  I want to type a short sequence of characters on the command line
(e.g., a variable name like "$sub") and then press a key (e.g., tab) that
replaces what I've typed with the value of the variable.  And I want this to
be done while I'm still typing on the command line -- that is, before I've
submitted the command.  Is there a way to do this with aliases or functions?

Thank you,

"Chris F.A. Johnson" <> wrote in message

 Sun, 03 Feb 2008 07:26:34 GMT   
 variable expansion on the bash command line
On 2005-08-16, John Bullock <> wrote:

In that case you want to look at the readline documentation. In
particular you should be interested in the 'shell-expand-line' function
which is bound by default to escape control-e (meta control-e).

To do what you want you would type
        $ s u b escape control-e

but you can bind it to a different key if you like, using the 'bind'

 Sun, 03 Feb 2008 08:23:00 GMT   
 variable expansion on the bash command line
On 2005-08-17, Icarus Sparry <> wrote:

Following up to myself, I find this is very useful when I am editing
things like my PATH variable. I might do something like


then think "oops, I men gnu not gnus", so I would type

and then expand it using escape control-e, then use the normal editing
keys to remove the s.

 Sun, 03 Feb 2008 08:26:20 GMT   
 variable expansion on the bash command line

It works!  Thank you.


 Sun, 03 Feb 2008 08:42:38 GMT   
 variable expansion on the bash command line


autocompletion is literally what it's name might suggest, you start to type
something and it's completed for you.
This is usually enabled for simple things such as file and directory names,
and by default it will be activated by the "TAB" key.

For example if you wish to look at the system's password file you might wish
to run:

less /etc/passwd
To save keystrokes you can actually type:

lesTAB /eTAB/passTAB
:In the Debian bash package there is a file installed called
/etc/bash_completion, this adds a lot more useful behaviours to bash
including:a.. Auto completion of hostnames, for SSH
  b.. Auto completion of Debian specific utilities
To cause your shell to use it run the following command, then login again:

echo '. /etc/bash_completion' >> ~/.bashrc
This will now give you a lot more completions, most usefully I find the
following :

apt-get upgTAB        ...Instead of typing out the filename we can take
advantage of another of bash's shortcuts - it remembers the last argument to
the previous command, and allows you to insert it into the current shell
with "Esc .".Run the following to see how it works:cat /etc/passwd
cp ESC. .
(That is press Esc, then press '.' afterwards' - the last argument to the
previous command is inserted into the command line).

 Sat, 09 Feb 2008 11:45:54 GMT   
 variable expansion on the bash command line


The "insert-last-arguement" readline function, which is bound to Meta-.
and Meta-_ has nothing to do with the bash_completion package, it is
standard readline.

The bash_completion is stuff is over 9,000 lines of shell functions which
you can extend the tab completion. Typically this uses builtins such as
compgen. It goes a long way beyond just being able to show hostnames for
ssh, you can have any completion you want. You could arrange for
completion to connect into a remote library system and return a list of
books that you have on loan if you wanted.

 Sat, 09 Feb 2008 22:34:29 GMT   
   [ 9 post ] 

Similar Threads

1. HELP:bash: making tilde expansion after command expansion...

2. Passing variables on the command line to a bash script

3. bash and terminating background processes with variable substitutions on command line

4. tilde + variable expansion in bash

5. tilde expansion with variable in bash script

6. Multiple variable expansion in bash. (ie: ${$1})

7. shell variables/command substitution/expansion

8. bash: expansion on the command line: different quoting rules for variables and aliases?

9. Solaris 2.6 Command line Expansion

10. typed filename expansion on command line

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software