Score:0

Create alias with variable input?

bz flag

So I've seen a similar question to this, but using fish. I though am using vanilla ubuntu 20 terminal.

What I have is an alias for a directory 1dir='cd such/and/such/and/such', and another for running a python script, where it needs an input of a file which is variable.

pyGraph='python3 -m bruhPy [filename.py]'
  1. Thing is - I obviously don't just always run the same filename.py. How do I make sure I can write pyGraph filename.py? Will that work?
  2. Can the 2 aliases be combined in one command? Such as 1dir pyGraph filename.py? Or do they have to be executed separately?
guiverc avatar
cn flag
Ubuntu 20? So you're using a Ubuntu Core 20 server system? (the *year* products don't forget are different products to the more widely used *year.month* systems; ie. 20 & 20.04 are different Ubuntu products).
Score:0
cn flag
  1. You'll write alias pyGraph='python3 -m bruhPy. Then pyGraph file.py is expanded to python3 -m bruhBy file.py

  2. Aliases are only expanded for the first word of a command. You'll need to write 1dir and pyGraph ... as 2 separate command (with a semicolon or newline separating them)

Score:0
us flag

What you can also do is write a function:

function pyg {
  cd such/and/such/and/such
  python3 -m bruhPy "$1"
}

and call it using pyg <filename>.

I find that I resort to functions whenever my aliases need more power.

user20982182 avatar
bz flag
Looks grand. I've got another answer with a function, but it's quite longer, and I don't really see why, when yours seems so straight forward, and sufficient. What do you think about it? . `go_and_run() { [[ -z "$1" ]] && { echo "No argument provided."; return 1; } cd such/and/such/and/such; python3 -m bruhPy "$1"; }`
MSpreij avatar
us flag
That function uses more errorchecking (which is good): "did the user supply a file". I considered adding a `if [[ -f "$1" ]] ...` which checks for a file at that location, which will also make it longer. You can also check whether the file is readable and such. If you like I can update the function with more error checking and comments.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.