Course of Raku / Objects, I/O, and exceptions / Input and output / Running external programs 🆕 / Exercises / Run a command
Solution: Run a command
Here is a possible solution to the task.
Code
run 'echo', 'home:', '$HOME';🦋 You can find the source code in the file run-a-command.raku.
Output
home: $HOMEComments
runlaunchesechodirectly, passing each string as a separate argument. With no:out, the program’s output goes straight to the screen.The argument reaches
echoexactly as written:$HOMEis printed literally, not replaced with your home directory, because no shell is involved to expand it. This is whyrunis the safe default — the same command given toshellcould behave very differently.To actually print the home directory, let a shell do the expansion:
shell 'echo home: $HOME'prints something likehome: /home/anna. (Keep the single quotes on the Raku side, too — a double-quoted"$HOME"would make Raku itself try to interpolate a variable named$HOMEbeforeechoever runs.)