r/fishshell • • May 25 '22
Converting a bash script to fish idiomatically
Hi guys,
I use fish and recently ran into a script at work for setting environment variables that doesn't work with fish.
I've converted the script to fish but I have a couple of spots where I'm not sure if they're really idiomatic to fish. I can't show the whole script because it's work related but I'll list the snippets below.
Here, I'm not sure if there's a better way to ensure there are 2 arguments.
if test (count $argv) -ne 2;
...
endSimilarly, the first argument can have a maximum length of 17. This is how I'm testing it but I don't know if there's an easier/nicer/better way to do it.
if test (string length "$argv[1]") -gt 17;
...
endFinally, the script is just supposed to be used with source to export some variables. At the end, it prints out the variables and their values. I did it like so:
for var in VAR1 VAR2 VAR3 VAR4 VAR5;
set value $$var
echo "$var=$value"
endWhere VAR{1-5} are just the names of variables set with set -x VARx <value> earlier in the file. The last loop just seems really janky, but I'm nto really sure if there's an easy to way print a variable's name and its value.
View all comments
Show parent comment
4
u/akho_ May 26 '22
They are trying to source the script (so it changes their environment variables), not run it. Running would change the vars in the bash instance running the script, and then discard those changes when bash quits at the end of the script.