0

Using bash on Ubuntu 22, I want to make a simple alias command.
It would be something like this: alias fuba='export tgt_foo=$1;touch ${tgt_foo}.log'

So, if I run it like ">$ fuba trial7", then the filename "trial7.log" will be touched, and I can refer the $tgt_foo variable afterwords...

I wish this would be simple and easy, but failed in many ways.;;;
Some experts explain it with "making function", but I feel like "not that much ;-("... Is there simple way to make it?
Please give me some clue.

Thank you for reading till here.

AJAMC
  • 17
  • 6
  • Use a function instead. – Jetchisel Jun 02 '23 at 10:07
  • Separately, having a function `export` a variable by default seems like a bad idea. Are you really sure this variable will need to be `export`ed? Do you know what it means? – tripleee Jun 02 '23 at 10:20
  • An alias has no $1. It would expand $1 from the enclosing context. Since an alias is usually only used interactively, it would mean that $1 is typically empty. Do a `set -u` for catching this kind of error. – user1934428 Jun 02 '23 at 12:55
  • instead of going alias fubar=, do.... fubar() { xxx } ... I don't even know whay aliases exist, they're completely redundant and pointless. – xpusostomos Jun 04 '23 at 06:09
  • ok. thank you for all yours answers. I didn't want to use function because it feels like dependant to certain shell. By the way, I prefer using "export" environment variable (always choosing quite unique names) also because it feels like independent to certain shell. Of course I refer that variable afterwords when I type cli commands, even in children-shells. And, sometimes I have to port the script file to Win10 system BAT file. That's why I preferred ENV VAR instead of bash function. Anyway, now I got it. everybody prefers function. I have to try not to use ENV VAR anymore, old man. – AJAMC Jun 05 '23 at 05:19

0 Answers0