0

How can I pass arguments to a command in unix? For example, if I have to open a file:

R> vi john/pic/mars/NASA/rover.txt 

In the above vi command, I want to replace "mars" with a variable, and pass the variable value in the same line, as in:

R> vi john/pic/$variable/NASA/rover.txt | $varaiable=pluto 

Of course this doesn't work. But I hope my question is clear. Can anyone help me with this?

2
  • @n.m. I would have thought so, too, but the variable actually only gets defined in the launched process itself, not the current command-line (in Bash 4.2.45(1); probably a feature). Have you actually tried it (without the typo?). Commented Apr 8, 2014 at 12:12
  • Sorry about the typo. Yes, I have tried without typo also. Commented May 1, 2014 at 8:08

1 Answer 1

1

Simply move the variable definition to the beginning of the command line, as in:

variable=pluto; vi john/pic/$variable/NASA/rover.txt 

or even:

variable=pluto && vi john/pic/$variable/NASA/rover.txt 

OBS:

  1. notice you can't use $ while defining variables, only while using their values;
  2. piping your vi command to a variable assignment does not make much sense, although you could achieve some clearer parameterization from:

    function opener() { vi john/pic/$1/NASA/rover.txt } $ opener "pluto" 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.