MCPcopy Index your code
hub / github.com/codeaashu/claude-code / findSuitableShell

Function findSuitableShell

src/utils/Shell.ts:73–137  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

71 * Determines the best available shell to use.
72 */
73export async function findSuitableShell(): Promise<string> {
74 // Check for explicit shell override first
75 const shellOverride = process.env.CLAUDE_CODE_SHELL
76 if (shellOverride) {
77 // Validate it's a supported shell type
78 const isSupported =
79 shellOverride.includes('bash') || shellOverride.includes('zsh')
80 if (isSupported && isExecutable(shellOverride)) {
81 logForDebugging(`Using shell override: ${shellOverride}`)
82 return shellOverride
83 } else {
84 // Note, if we ever want to add support for new shells here we'll need to update or Bash tool parsing to account for this
85 logForDebugging(
86 `CLAUDE_CODE_SHELL="${shellOverride}" is not a valid bash/zsh path, falling back to detection`,
87 )
88 }
89 }
90
91 // Check user's preferred shell from environment
92 const env_shell = process.env.SHELL
93 // Only consider SHELL if it's bash or zsh
94 const isEnvShellSupported =
95 env_shell && (env_shell.includes('bash') || env_shell.includes('zsh'))
96 const preferBash = env_shell?.includes('bash')
97
98 // Try to locate shells using which (uses Bun.which when available)
99 const [zshPath, bashPath] = await Promise.all([which('zsh'), which('bash')])
100
101 // Populate shell paths from which results and fallback locations
102 const shellPaths = ['/bin', '/usr/bin', '/usr/local/bin', '/opt/homebrew/bin']
103
104 // Order shells based on user preference
105 const shellOrder = preferBash ? ['bash', 'zsh'] : ['zsh', 'bash']
106 const supportedShells = shellOrder.flatMap(shell =>
107 shellPaths.map(path => `${path}/${shell}`),
108 )
109
110 // Add discovered paths to the beginning of our search list
111 // Put the user's preferred shell type first
112 if (preferBash) {
113 if (bashPath) supportedShells.unshift(bashPath)
114 if (zshPath) supportedShells.push(zshPath)
115 } else {
116 if (zshPath) supportedShells.unshift(zshPath)
117 if (bashPath) supportedShells.push(bashPath)
118 }
119
120 // Always prioritize SHELL env variable if it's a supported shell type
121 if (isEnvShellSupported && isExecutable(env_shell)) {
122 supportedShells.unshift(env_shell)
123 }
124
125 const shellPath = supportedShells.find(shell => shell && isExecutable(shell))
126
127 // If no valid shell found, throw a helpful error
128 if (!shellPath) {
129 const errorMsg =
130 'No suitable shell found. Claude CLI requires a Posix shell environment. ' +

Callers 1

getShellConfigImplFunction · 0.85

Calls 4

isExecutableFunction · 0.85
logForDebuggingFunction · 0.85
logErrorFunction · 0.70
pushMethod · 0.45

Tested by

no test coverage detected