Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change shell used to run "shell commands" in GHCi

Tags:

haskell

ghci

In GHCi, a shell command can be executed using :!<command>. By default, it seems that the sh shell is used:

Prelude> :!echo $0
/bin/sh

I have a number of aliases and custom functions defined for the shell I use generally on my system (fish), and I'd like to use these from GHCi but not have to translate them into sh-compatible versions.

Is there a way of changing the shell GHCi uses to execute :! commands? I haven't been able to find any :set options for this, nor any command-line arguments to GHCi to do this.

like image 919
hnefatl Avatar asked Sep 08 '17 12:09

hnefatl


1 Answers

The :! command in current versions of ghci is a call to System.Process.system

-- | Entry point for execution a ':<command>' input from user
specialCommand :: String -> InputT GHCi Bool
specialCommand ('!':str) = lift $ shellEscape (dropWhile isSpace str)
-- [...]

shellEscape :: String -> GHCi Bool
shellEscape str = liftIO (system str >> return False)

I suspect system is hardcoded to /bin/sh, but there's no reason you can't define your own ghci command to do fish calls. I don't have fish installed, so I'll use bash as an example:

λ import System.Process (rawSystem)
λ :def bash \cmd -> rawSystem "/bin/bash" ["-c", cmd] >> return ""
λ :bash echo $0
/bin/bash
like image 182
rampion Avatar answered Sep 28 '22 10:09

rampion