MCPcopy Create free account
hub / github.com/AnukarOP/claude-code-leaked / findSuitableShell

Function findSuitableShell

source code/utils/Shell.ts:75–139  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

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

Callers 1

getShellConfigImplFunction · 0.85

Calls 3

isExecutableFunction · 0.85
logForDebuggingFunction · 0.85
logErrorFunction · 0.70

Tested by

no test coverage detected