https://github.com/albertalef/rubyshell
https://github.com/albertalef/rubyshell
automation hacktoberfest ruby script shell shell-script
Last synced: about 8 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/albertalef/rubyshell
- Owner: albertalef
- License: mit
- Created: 2025-10-01T02:00:39.000Z (4 months ago)
- Default Branch: master
- Last Pushed: 2026-01-20T16:04:57.000Z (3 days ago)
- Last Synced: 2026-01-21T00:18:41.076Z (3 days ago)
- Topics: automation, hacktoberfest, ruby, script, shell, shell-script
- Language: Ruby
- Homepage: https://rubygems.org/gems/rubyshell
- Size: 1.12 MB
- Stars: 56
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
✨ Rubist way to create shell scripts ✨
Installation
·
Usage
·
Examples
·
Contributing
·
Sponsors
```ruby
cd "/log" do
ls.each_line do |line|
puts cat(line)
end
end
```
Yes, that’s valid Ruby!
`ls` and `cat` are just shell commands, but **RubyShell** makes them behave like Ruby methods.
## Installation
Install the gem and add to the application's Gemfile by executing:
$ bundle add rubyshell
If bundler is not being used to manage dependencies, install the gem by executing:
$ gem install rubyshell
## Usage
### Calling a shell command
With RubyShell, every shell command can be used inside the ruby, you just need to call
```ruby
sh do
puts pwd # => /Users/albertalef/projects/rubyshell
end
```
### Using without the block
If you want to start an irb or pry session, and run commands as first-class citizens then do the following.
```ruby
extend RubyShell::Executor
pwd # => /Users/albertalef/projects/rubyshell
```
### Passing arguments
Here we have different ways to pass arguments to a command.
You can separate strings, use only one, use hashes, anyway will work
```ruby
sh do
docker("ps", all: true) # Using hash syntax = docker ps --all
docker("ps", a: true) # Using hash syntax = docker ps -a
docker("ps", '-a') # Passing multiple strings = docker ps -a
docker("ps -a") # Passing one string = docker ps -a
end
```
### Changing folder
Has two possible ways, changing the folder of the code, or running code only inside a folder
##### Chaging code folder
```ruby
sh do
puts pwd # => /Users/albertalef/projects/rubyshell
cd 'examples'
puts pwd # => /Users/albertalef/projects/rubyshell/examples
end
```
##### Executing code inside another folder
```ruby
sh do
cd 'examples' do
puts pwd # => /Users/albertalef/projects/rubyshell/examples
end
puts pwd # => /Users/albertalef/projects/rubyshell
end
```
### Chaining commands
The `chain` method make possible we use shell operators inside the ruby, like `& && | > >> < <<`
```ruby
sh do
chain { echo "Dummy text" >> "dummy.txt" }
puts cat("dummy.txt") # => "Dummy text"
end
sh do
number_of_files = chain { ls | wc('-l') }.chomp
puts number_of_files # => 5
end
```
### Executing without a block
The `sh` method can receive any method call, and execute shell commands
```ruby
sh.puts pwd # => /Users/albertalef/projects/rubyshell
sh.cd 'examples'
puts sh.pwd # => /Users/albertalef/projects/rubyshell/examples
```
## Complete example
```ruby
#!/usr/bin/env ruby
require "rubyshell"
require "securerandom"
sh do
mkdir "files"
cd "files" do
5.times do |i|
chain do
echo(SecureRandom.alphanumeric(16)) >> "#{i}.txt"
end
end
puts "Number of Files: #{ls.lines.count}"
ls.each_line do |filename|
puts cat(filename)
end
end
ensure
rm "-rf files"
end
# Running:
#
# ❯ ./examples/example1.rb
#
# Number of Files: 5
# o6Kw8KHvWJnLGSeQ
# qkRKcZHqu2Moq1se
# nUPluln9GM1ydtoz
# rkdYsc1RBhkeN1dq
# ZPXZMqzYfyFfjPHF
```
## Coming
- Support to Streams
## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/albertalef/rubyshell. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/albertalef/rubyshell/blob/master/CODE_OF_CONDUCT.md).
## Sponsors
## License
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
## Code of Conduct
Everyone interacting in the Rubysh project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/albertalef/rubyshell/blob/master/CODE_OF_CONDUCT.md).