1

Within .bashrc, I define a function: mipinfo () {...}

function mipinfo () {
    outfile="~/path/`date +%Y%m%d`_mipinfo"
    printf "%s\n" "$outfile"; touch $outfile
    .....
}

Sourcing .bashrc results in exit value 0.

Typing mipinfo in Gnome terminal, I get:

~/path/20150807_mipinfo
touch: cannot touch ‘~/path/20150807_mipinfo’: No such file or directory
bash: ~/path/20150807_mipinfo: No such file or directory

I guess this has to do with the parameter $outfile being local and not being passed to the calling shell where the execution of touch takes effect. I realize that to pass a called script's variables back to the calling shell one needs to source the called script in that shell, but I am not clear how I can circumvent that for a simple function using bash scripting only (not C++, as here).

It seems to be a case of passing the pertinent variable $outfile from within the function to the calling script/shell (and not the usual opposite). Not even sure about that actually. Any pointer greatly appreciated...

Community
  • 1
  • 1
Cbhihe
  • 511
  • 9
  • 24
  • 2
    Tilde does not expand in quotes. Use $HOME. (This is literally output from [shellcheck](http://www.shellcheck.net) when executed on your script). – that other guy Aug 07 '15 at 21:58
  • Wow :-| Really embarrassing, but thanks @that_other_guy ! I wasted 90min of my life and 10s of yours on this. I guess nothing replaces reading and rereading `man bash`. Please put it up as an answer so I can mark it so. Cheers, – Cbhihe Aug 07 '15 at 22:06
  • This is only peripheral to the function doing its job, but is it at all possible to pass a variable value from within a function to the calling script/shell in bash ? – Cbhihe Aug 07 '15 at 22:09
  • 1
    Nothing replaces reading/rereading man bash, but as demonstrated, shellcheck is a good shortcut for a lot of it (you can install it in your editor for instant feedback). And yes, canonically you would capture the output of your function like `var=$(mipinfo)` but `outfile` is global by default and would be set once the function finishes – that other guy Aug 07 '15 at 22:12
  • +1 for shellcheck, @that_other_guy. Incredibly useful tool; tx a bunch ! I just went through a 46 line bash script that gave me all sort of very bad pains all over in under 6 minutes. It even suggests improvement for correct by legacy notations ! Local install a must. – Cbhihe Aug 07 '15 at 22:36
  • @that_other_guy: The error was clearly a duplicate, but was the question ? – Cbhihe Aug 07 '15 at 22:38
  • I upvoted this sir as his question pinpoints the actual problem precisely, description of the problem is comprehensive, rather than the [“~/Desktop/test.txt: No such file or directory”](http://stackoverflow.com/questions/8409024/desktop-test-txt-no-such-file-or-directory) – NarūnasK Aug 07 '15 at 22:55
  • @Cbhihe The answer to the explicit quesiton is just "same as from anywhere else, `touch somefile`", but that seemed much less pragmatic and helpful than the answer to the implied question of "why does this function fail to create the file I want?". It was just my attempt at maximizing helpfulness, and if you have a different opinion I will whole-heartedly support a reopen or repost! – that other guy Aug 07 '15 at 23:16
  • @Narünas_K: Very nice of you as it's not usual to upvote something marked as duplicate. – Cbhihe Aug 08 '15 at 07:40
  • @that_other_guy: No, leave it as is. I respect your opinion and you have a strong case anyway. Beyond that you _did_ answer the fundamental question I had, which was whether `outfile` is global or not when defined inside a function. I can build on that, which is the only thing that counts. That also is the reason I would have liked to adorn your answer with a nice green check. Thanks again. – Cbhihe Aug 08 '15 at 07:45

0 Answers0