user13215247
user13215247

Reputation:

Make A File-Specific Bash Alias

I'm on a mac and I've recently found out about bash aliases. Take this following one as an example:

# Python shortcut
alias c.py="touch template.py && open template.py"

It creates a python file and then immediately opens it. I think It's a useful tool, but I'd like to improve it slightly. Every time this is run, it creates a file by the name 'template.py', which is fine as I can save it under a different name, however I'd like to be able to specify the name within the command itself. For example, something like:

alias c.py(filename)="touch filename.py && open filename.py" 

How could I do this? Thanks in advance.

Upvotes: 0

Views: 39

Answers (2)

Peaceful James
Peaceful James

Reputation: 2233

Define it as a function and alias to that function. Use $1 in the function to access "first argument"

function c.py() {
  touch $1.py
  open $1.py
}

alias c.py=c.py

Upvotes: -1

chepner
chepner

Reputation: 531718

You want a function, not an alias.

editpy () {
  touch "$1.py" && open "$1.py"
}

Then you can run editpy template or editpy newfile or whatever you want. $1 will expand to the first argument that the function is called with.

Upvotes: 2

Related Questions