Class: Ukiryu::Shell::Bash

Inherits:
UnixBase show all
Defined in:
lib/ukiryu/shell/bash.rb

Overview

Bash shell implementation

Bash uses single quotes for literal strings and backslash for escaping. Environment variables are referenced with $VAR syntax.

Constant Summary collapse

SHELL_NAME =
:bash
EXECUTABLE =
'bash'

Constants inherited from UnixBase

UnixBase::PLATFORM

Constants inherited from Base

Ukiryu::Shell::Base::PLATFORM, Ukiryu::Shell::Base::SPECIAL_CHARS_PATTERN, Ukiryu::Shell::Base::WHITESPACE_PATTERN

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from UnixBase

#execute_command, #execute_command_with_stdin, #shell_executable

Methods inherited from Base

#capabilities, #encoding, #environment_to_h, #execute_command, #execute_command_with_stdin, #format_environment, #format_path, #needs_quoting?, #supports?

Class Method Details

.detect_alias(command_name) ⇒ Hash?

Detect if a command is a Bash alias

Uses the ‘type’ builtin which returns “X is alias Y” for aliases.

Parameters:

  • command_name (String)

    the command to check

Returns:

  • (Hash, nil)

    “…”, target: “…” or nil if not an alias



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/ukiryu/shell/bash.rb', line 19

def self.detect_alias(command_name)
  # Use 'type' builtin which returns "alias is alias" for aliases
  result = `type #{command_name} 2>/dev/null`
  return nil unless result

  if result =~ /^#{command_name} is alias (.*)$/
    alias_definition = ::Regexp.last_match(1)
    # Extract target from alias definition
    # Format: alias ll='ls -l'
    if alias_definition =~ /^'(.*)'$/
      target = ::Regexp.last_match(1)
      target = extract_command_from_alias(target)
      { definition: result.strip, target: target }
    end
  end
  nil
end

.extract_command_from_alias(alias_def) ⇒ String

Extract the first word from an alias definition

Parameters:

  • alias_def (String)

    the alias definition

Returns:

  • (String)

    the first word (command)



41
42
43
44
45
# File 'lib/ukiryu/shell/bash.rb', line 41

def self.extract_command_from_alias(alias_def)
  # Extract the first word from the alias definition
  # e.g., "ls -l --color=auto" -> "ls"
  alias_def.split(/\s+/).first
end

Instance Method Details

#env_var(name) ⇒ String

Format an environment variable reference

Parameters:

  • name (String)

    the variable name

Returns:

  • (String)

    the formatted reference ($VAR)



81
82
83
# File 'lib/ukiryu/shell/bash.rb', line 81

def env_var(name)
  "$#{name}"
end

#escape(string) ⇒ String

Escape a string for Bash Single quotes are literal (no escaping inside), so we end the quote, add an escaped quote, and restart the quote.

Parameters:

  • string (String)

    the string to escape

Returns:

  • (String)

    the escaped string



64
65
66
# File 'lib/ukiryu/shell/bash.rb', line 64

def escape(string)
  string.to_s.gsub("'") { "'\\''" }
end

#headless_environmentHash

Get headless environment (disable DISPLAY on Unix)

For macOS, adds additional variables to prevent GUI initialization that can cause crashes in GUI applications like Inkscape.

Returns:

  • (Hash)

    environment variables for headless operation



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ukiryu/shell/bash.rb', line 100

def headless_environment
  env = {}

  # Completely remove DISPLAY instead of setting to empty string
  # This ensures full headless mode with no display connection
  # The executor will exclude this key from the environment entirely

  # Add macOS-specific environment variables to prevent GUI initialization
  if Ukiryu::Platform.detect == :macos
    env['NSAppleEventsSuppressStartupAlert'] = 'true' # Suppress Apple Events
    env['NSUIElement'] = '1' # Run as background agent
    env['GDK_BACKEND'] = 'x11' # Force X11 backend (respects missing DISPLAY)
  end

  env
end

#join(executable, *args) ⇒ String

Join executable and arguments into a command line

Parameters:

  • executable (String)

    the executable path

  • args (Array<String>)

    the arguments

Returns:

  • (String)

    the complete command line



90
91
92
# File 'lib/ukiryu/shell/bash.rb', line 90

def join(executable, *args)
  [quote(executable), *args.map { |a| quote(a) }].join(' ')
end

#nameObject



47
48
49
# File 'lib/ukiryu/shell/bash.rb', line 47

def name
  :bash
end

#quote(string) ⇒ String

Quote an argument for Bash Uses single quotes for literal strings

Parameters:

  • string (String)

    the string to quote

Returns:

  • (String)

    the quoted string



73
74
75
# File 'lib/ukiryu/shell/bash.rb', line 73

def quote(string)
  "'#{escape(string)}'"
end

#shell_commandString

Get the bash command name to search for

Returns:

  • (String)

    the bash command name



54
55
56
# File 'lib/ukiryu/shell/bash.rb', line 54

def shell_command
  'bash'
end