You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
`ctx.$` is a tagged template literal function that provides a shell-like syntax for executing commands. It's also available as a standalone export `$` from foy.
384
+
385
+
```ts
386
+
387
+
task('build', asyncctx=> {
388
+
// Using ctx.$ (same as standalone $)
389
+
awaitctx.$`tsc`
390
+
awaitctx.$`node ./test.js`
391
+
392
+
// Variable interpolation - values are automatically escaped
393
+
const file ='my file.txt'// note the space in filename
394
+
awaitctx.$`cat ${file}`// safely escapes to: cat 'my file.txt'
395
+
396
+
const count =5
397
+
awaitctx.$`echo ${count}`// outputs: 5
398
+
399
+
// Using with options
400
+
const { stdout } =awaitctx.$`ls -la`
401
+
console.log(stdout)
402
+
403
+
// Complex example with multiple variables
404
+
const src ='./src'
405
+
const dest ='./dist'
406
+
awaitctx.$`cp -r ${src} ${dest}`
407
+
})
408
+
```
409
+
410
+
**Key features of `ctx.$`:**
411
+
-**Automatic escaping**: Variables are safely escaped for shell execution
412
+
-**Template literal syntax**: More readable and familiar for shell commands
413
+
-**Returns execa result**: Access `stdout`, `stderr`, `exitCode`, etc.
414
+
-**Default options**: `stdio: 'inherit'` and `shell: true` are set by default
415
+
416
+
**Differences from `ctx.exec`:**
417
+
418
+
```ts
419
+
// ctx.$ - template literal syntax, auto-escaping
420
+
awaitctx.$`echo ${userInput}`// safely escapes special characters
421
+
422
+
// ctx.exec - string syntax, no auto-escaping
423
+
awaitctx.exec(`echo ${userInput}`) // be careful with special characters!
0 commit comments