3

I'm trying to create an alias that will create a file and open it in VS Code.

Create an alias called create <filename> that will execute touch <filename> && code <filename>.

For example create app.js should execute touch app.js && code app.js.

0

1 Answer 1

2

From man bash, under ALIASES:

There is no mechanism for using arguments in the replacement text. If arguments are needed, a shell function should be used (see FUNCTIONS below)."

Thus:

create() { touch "$1"; code "$1"; }
Sign up to request clarification or add additional context in comments.

7 Comments

You should almost always double-quote variables (and parameters). Also, the function keyword is nonstandard; I'd recommend the syntax create() { touch "$1"; code "$1"; }
@GordonDavisson: I agree about quoting. I disagree about function: the tag is bash, not sh, and so the standard is whatever man bash says.
man bash says the function keyword is optional. Putting it there anyway simply makes your code less portable.
@tripleee: I guess I should also replace all x++ in C with x += 1 because Ruby can't understand ++, just in case... Fine...
That's reductio ad absurdum. Shell scripts can be portable or specific to a particular shell. If you need features from a particular shell then of course striving for portability doesn't make any sense; but gratuitously wrecking otherwise portable code is just lazy.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.