# Without using Finder to change a preference #

Focusing on the command line …

## Preparation ##

Two commands: 

`defaults read com.apple.LaunchServices | grep -B 1 -A 3 public.shell-script`

`defaults read com.apple.LaunchServices | grep -B 1 -A 4 "LSHandlerContentTag = sh;"`

### If either command reveals an existing preference ###

Use Xcode or any other suitable property list editor to remove the relevant dictionary from the `LSHandlers` key within the following file: 

`~/Library/Preferences/com.apple.LaunchServices.plist`

Hints: 

- command line removal of a single dictionary from an array may be unnecessarily difficult
- removal should be necessary only if (say) the user has previously used Finder to associate `.sh` files with an application other than the system default.

## Adding to LaunchServices preferences, for Terminal to view and edit .sh files ##

Two one-line commands. 

Depending on your requirements, the first command alone may suffice: 

` defaults write com.apple.LaunchServices LSHandlers -array-add '{ LSHandlerContentType = "public.shell-script"; LSHandlerRoleViewer = "com.apple.Terminal"; LSHandlerRoleEditor = "com.apple.Terminal"; }'`

`defaults write com.apple.LaunchServices LSHandlers -array-add '{ LSHandlerContentTag = "sh"; LSHandlerContentTagClass = "public.filename-extension"; LSHandlerRoleViewer = "com.apple.Terminal"; LSHandlerRoleEditor = "com.apple.Terminal"; }'`

### For Finder to respect those new preferences ###

Maybe not the best approach for modern releases of the system, but these two commands seem effective: 

`/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill`

`/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -seed -v`

- option `-v` only if you want a verbose view of progress. 

After the scan completes — after the Launch Services database has been seeded — you should find that `.sh` files open in Terminal in response to an `open` command. 

## Background ##

### Terminal.app support for .sh (public.shell-script) ###

Consider the result of the following command: 

`defaults read /Applications/Utilities/Terminal.app/Contents/Info`

- under `CFBundleDocumentTypes` ([an array of dictionaries describing the document types supported by the bundle][1]) we see that Terminal is designed to support `com.apple.terminal.shell-script` 

- `public.shell-script ` (the [system-declared UTI][2] associated with `.sh`) appears elsewhere in the same property list — under `UTExportedTypeDeclarations`

### Credit ###

If the first edition of this answer is heading in the right direction, I'll add references/links …


  [1]: https://developer.apple.com/library/mac/#documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
  [2]: http://developer.apple.com/library/ios/#documentation/Miscellaneous/Reference/UTIRef/Articles/System-DeclaredUniformTypeIdentifiers.html